Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert string with commas to float

Is there a built-in Delphi function which would convert a string such as '3,232.00' to float? StrToFloat raises an exception because of the comma. Or is the only way to strip out the comma first and then do StrToFloat?

Thanks.

like image 735
M Schenkel Avatar asked Apr 25 '09 02:04

M Schenkel


People also ask

How do you convert a string to a comma with a float in python?

To convert a string with comma separator and dot to a float:Use the str. replace() method to remove the commas from the string. Use the float() class to convert the string to a floating-point number.

How do you convert a string to a float?

We can convert a string to float in Python using the float() function. This is a built-in function used to convert an object to a floating point number. Internally, the float() function calls specified object __float__() function.

How do you convert a string to a number if it has commas in it as thousands separators?

replace() method. To convert a string with a comma as the thousands separator to a number: Use the str. replace() method to remove the commas from the string.


1 Answers

Do you exactly know, that '.' is decimal separator and ',' is thousand separator (always)? If so, then you should fill the TFormatSettings record and pass it to StrToFloat.

FillChar(FS, SizeOf(FS), 0);
... // filling other fields
FS.ThousandSeparator := ',';
FS.DecimalSeparator := '.';
V := StrToFloat(S, FS);
like image 159
Alex Avatar answered Sep 19 '22 13:09

Alex