Vaalar
asked on
how to separate the float number from string
Hello Guys,
Can you tell me how can I receive only a float number from the string. For example:
string - 252.676g or f466.422d
BR
Vaalar
Can you tell me how can I receive only a float number from the string. For example:
string - 252.676g or f466.422d
BR
Vaalar
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Geert_Gruwez, your code doesnt take into account if there is a non numerical character at the end of the string.
no need the Val does this
the aCode will show the location of the problem
maybe Vaalar can evaluate further what he wants to happen ?
maybe Vaalar can evaluate further what he wants to happen ?
In fact it might be better to make sure you never continue past the first number that is found
function TForm1.returnFloat(const s: string): double;
var
i, startPos, endPos: integer;
begin
result := 0;
startPos := 1;
while ( not ( s[ startPos] in ['0'..'9', DecimalSeparator,ThousandS eparator, '+', '-'])) and
( startPos <= length( s)) do
inc( startPos);
endPos := startPos;
while ( s[ endPos] in ['0'..'9', DecimalSeparator,ThousandS eparator, '+', '-']) and
( endPos <= length( s)) do
inc( endPos);
if endPos >= startPos then
tryStrToFloat( copy( s, startPos, endPos - startPos), result)
end;
function TForm1.returnFloat(const s: string): double;
var
i, startPos, endPos: integer;
begin
result := 0;
startPos := 1;
while ( not ( s[ startPos] in ['0'..'9', DecimalSeparator,ThousandS
( startPos <= length( s)) do
inc( startPos);
endPos := startPos;
while ( s[ endPos] in ['0'..'9', DecimalSeparator,ThousandS
( endPos <= length( s)) do
inc( endPos);
if endPos >= startPos then
tryStrToFloat( copy( s, startPos, endPos - startPos), result)
end;
ASKER
Thx for your help.
I have tested your ideas and it solves but if i have for example 244e.322 i get 244 afer using Gerts solution. I need to receive 244.322.
The same problem comes out in Mikes solution.
I have tested your ideas and it solves but if i have for example 244e.322 i get 244 afer using Gerts solution. I need to receive 244.322.
The same problem comes out in Mikes solution.
ASKER
Thx for your help.
I have tested your ideas and it solves some problems but if i have for example 244e.322 i get 244 afer using Gerts solution. I need to receive 244.322.
The same problem comes out in Mikes solution.
I have tested your ideas and it solves some problems but if i have for example 244e.322 i get 244 afer using Gerts solution. I need to receive 244.322.
The same problem comes out in Mikes solution.
you could first strip all non numerals:
function ConvertFloat(aText: string): Double;
var S: string;
I, aCode: Integer;
begin
// First delete all non number characters
I := 1;
s := '';
while (I <= Length(aText)) and (aText[I] in ['-','+',DecimalSeparator, ThousandSe parator,'0 '..'9']) do
begin
s := s + aText[I];
Inc(I);
end;
// Convert text to float
Val(S, Result, aCode);
end;
function ConvertFloat(aText: string): Double;
var S: string;
I, aCode: Integer;
begin
// First delete all non number characters
I := 1;
s := '';
while (I <= Length(aText)) and (aText[I] in ['-','+',DecimalSeparator,
begin
s := s + aText[I];
Inc(I);
end;
// Convert text to float
Val(S, Result, aCode);
end;
this will never solve all problems
it will get the first number possible
but to resolve this you'll need to specify some rules:
aText := 'abc123.def456-789+1023xxx ';
Value to string = '123.456-789+1023';
it will get the first number possible
but to resolve this you'll need to specify some rules:
aText := 'abc123.def456-789+1023xxx
Value to string = '123.456-789+1023';
woops slight mistake
function ConvertFloat(aText: string): Double;
var S: string;
I, aCode: Integer;
begin
// First delete all non number characters
I := 1;
s := '';
while (I <= Length(aText)) do
begin
if (aText[I] in ['-','+',DecimalSeparator, ThousandSe parator,'0 '..'9']) then
s := s + aText[I];
Inc(I);
end;
// Convert text to float
Val(S, Result, aCode);
end;
function ConvertFloat(aText: string): Double;
var S: string;
I, aCode: Integer;
begin
// First delete all non number characters
I := 1;
s := '';
while (I <= Length(aText)) do
begin
if (aText[I] in ['-','+',DecimalSeparator,
s := s + aText[I];
Inc(I);
end;
// Convert text to float
Val(S, Result, aCode);
end;
or try StrToFloatDef
Result := StrToFloatDef(S, 0);
Result := StrToFloatDef(S, 0);
ASKER
Thank you very much Geert,
and thx also to Mike
BR
Vaalar
and thx also to Mike
BR
Vaalar