Delphi xe5 StrToFloat failed to change ThousandSeparator to ','

What am I doing wrong here? I just want to convert the formatted string to double and use the TFormatSettings passed as the StrToFloat parameter. I get the following exception:

'3,332.1' is not a valid floating point value. 

The decimal separator and decimal separator are the expected values ​​(',' and '.') In TFormatSettings.

 procedure TForm2.Button1Click(Sender: TObject); var FS: TFormatSettings; S: String; V: double; begin FS:= TFormatSettings.Create; codesite.Send('ThousandSeparator', FS.ThousandSeparator); //correct ',' codesite.Send('DecimalSeparator', FS.DecimalSeparator); //correct '.' S := '3,332.1'; try V := StrToFloat(S, FS); except on E: Exception do ShowMessage(e.Message); end; CodeSite.Send('S', S); CodeSite.Send('V', V); end; 
+6
source share
2 answers

What you are doing here is correct, but you have stumbled upon what seems like a mistake (if not a mistake, at least not a very consistent behavior) TextToFloat (it seems that it ignores ThousandSeparator) Delphi SysUtils internal function unit (take a look at Q92265 to follow permission) ...

As a workaround, you can try removing the group separator as follows:

 StringReplace('3,332.1', ',', '', [rfReplaceAll]) 
+2
source

This behavior is also designed. From the documentation with an accent:

Use StrToFloat to convert the string S to a floating point value. S must consist of an optional sign (+ or -), a string of digits with an optional decimal point, and an optional mantissa. Mantissa consists of an ā€œEā€ or ā€œeā€ followed by an optional sign (+ or -) and an integer. Leading and trailing spaces are ignored.

The global variable DecimalSeparator or its equivalent TFormatSettings defines the character that is used as the decimal point. Separators of thousands of characters and currency characters are not allowed in the string . If S does not contain a valid value, StrToFloat raises an EConvertError exception.

So this is an error in passing a string containing a thousands separator to this function.

+9
source

Source: https://habr.com/ru/post/975241/


All Articles