EConvertError - not a valid floating point value

Hey there,

I got this error message
-1,251 is not a valid floating point value

when I do this
  sBal := sBal + StrToFloat(ADOQ.FieldByName('Balance').AsString)

I think it has to do something with the comma, how can sort this... :-/
LVL 10
wildzeroAsked:
Who is Participating?
 
Russell LibbySoftware Engineer, Advisory Commented:

If the field is already a numeric tyoe (BCD, etc), then you should skip the .AsString and use something like .AsFloat instead. If the field really is a TString type, then the following routine could be used to clean the string up before conversion (it uses the locale info to perform the cleanup). A little more code, but much faster....

Regards,
Russell


function LocaleStrToDouble(S: String): Double;
var  lpszConvert:   PChar;
     cCurrency:     Char;
     szConvert:     String;
     dwLength:      Integer;
begin

  // Get source string
  lpszConvert:=PChar(S);

  // Check string
  if (lpszConvert = nil) then
     // Zero result
     result:=0
  else
  begin
     // Allocate source string
     SetLength(szConvert, Length(S));
     // Set parsed length
     dwLength:=0;
     // Strip thousands sep char in the string
     while (lpszConvert^ > #0) do
     begin
        // Check for thousands sep char
        if not(lpszConvert^ = ThousandSeparator) then
        begin
           // Copy char
           szConvert[Succ(dwLength)]:=lpszConvert^;
           // Push length
           Inc(dwLength);
        end;
        // Push next
        Inc(lpszConvert);
     end;
     // Set parsed length
     SetLength(szConvert, dwLength);
     // Convert to double
     Val(szConvert, result, dwLength);
     // Check conversion
     if (dwLength <> 0) then raise EConvertError.Create('Invalid floating point string!');
  end;

end;
0
 
wildzeroAuthor Commented:
I could do this

// Remove the , from the passed string
Function stripcom ( item : string ) : string;
begin
  result := AnsiReplaceText(item, ',', '');
end;

and call
 sBal := sBal + StrToFloat( stripcom(ADOQ.FieldByName('Balance').AsString) );

but it doesn't quiet feel right...
0
 
wildzeroAuthor Commented:
>>but it doesn't quiet feel right...
I mean, im sure it could be done better (proper)
0
 
Kyle FosterCEOCommented:
use fieldbyname('').ascurrency
or
if the field is stored as a string in the database then change the field type if possible.
0
 
wildzeroAuthor Commented:
Thanks guys =)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.