Fórum como converter de char para string #297944

30/09/2005

0

eu estava querendo fazer uma funcao pra retornar soh os numeros de uma string

algo assim

function TForm1.StrToNum(Str:string):Real;
var
temp : string;
i: integer;
v: Real
begin
    FOR i:= 0 TO length(str)do
    begin
      temp := copy(str,i,(1));
      if temp in [´0..9´] then v:= v+temp;
    end;
   
  result = V
end;



se alguem puder me ajudar nessa funcao


Anderson_blumenau

Anderson_blumenau

Responder

Posts

30/09/2005

Rjun

function TForm1.StrToNum(Str: string): Double;
var
  Aux: string;
  i: integer;
begin
  Aux := ´´;
  for i := 1 to Length(Str) do
    if Str[i] in [´0´..´9´] then Aux := Aux + Str[i];

  if Aux <> ´´ then result := StrToFloat(Aux);
               else result := 0;
end;



Responder

Gostei + 0

30/09/2005

Anderson_blumenau

valeu pena ajuda amigo, e foi bem rapida

mas

se eu colocar um numero tipo R$ 0,25
a funcao me retorna 25, eu gostaria que ela me retornasse um numero real


Responder

Gostei + 0

30/09/2005

Rjun

Então altere a linha do if.

function TForm1.StrToNum(Str: string): Double; 
var 
  Aux: string; 
  i: integer; 
begin 
  Aux := ´´; 
  for i := 1 to Length(Str) do 
    if Str[i] in [´0´..´9´, ´,´] then Aux := Aux + Str[i]; 

  try
    result := StrToFloat(Aux); 
  except
    result := 0; 
 end; 
end; 



Responder

Gostei + 0

30/09/2005

Anderson_blumenau

claro

estupidez a minha nao ter visto isso

as vezes o cara fica meio cego e nao ve o mais simples

valeu cara


Responder

Gostei + 0

Utilizamos cookies para fornecer uma melhor experiência para nossos usuários, consulte nossa política de privacidade.

Aceitar