TextToFloat returns wrong value for Float when DecimalSeparator equals ThousandSeparator
Original Reporter info from Mantis: Bart @flyingsheep
-
Reporter name: Bart Broersma
Original Reporter info from Mantis: Bart @flyingsheep
- Reporter name: Bart Broersma
Description:
When DecimalSeparator equals ThousandSeparator (for instance when programmer sets DecimalSeparator but forgets to set ThousandSeparator to different value) then TextToFloat gives wrong results.
(This can make FloatSpinedit behave rather curious, see #13293)
Steps to reproduce:
Set DecimalSeparator to '.'
Set ThousandSeparator to '.'
Var Value: Extended;
TextToFloat(PChar('123.456'), Value, fvExtended, DefaultFormatSettings) returns True (correct) and Value will be 123456.000 (incorrect)
I consider this a bug, because it is incompatible with Delphi (version 3), which in this case sets value to 123.456
I can attach a sample program demonstrating the behaviour if needed.
If agreed I can create a diff.
Additional information:
The cause is the removal of ThousandSeparator which in this case is also DecimalSeparator, so no DecimalSeparator is left in place.
Possible fix for this issue:
Function TextToFloat(Buffer: PChar; Var Value; ValueType: TFloatValue; Const FormatSettings: TFormatSettings): Boolean;
Var
E,P : Integer;
S : String;
{$ifndef FPC_HAS_STR_CURRENCY}
TempValue: extended;
{$endif FPC_HAS_STR_CURRENCY}
Begin
S:=StrPas(Buffer);
//Only remove ThousandSeparator if <> DecimalSeparator: Delphi compatibility
if (FormatSettings.ThousandSeparator <> FormatSettings.DecimalSeparator) then
begin
P:=Pos(FormatSettings.ThousandSeparator,S);
While (P<>0) do
begin
Delete(S,P,1);
P:=Pos(FormatSettings.ThousandSeparator,S);
end;
end;
...
Function MyTextToFloat(Buffer: PChar; Var Value: Extended; Const FormatSettings: TFormatSettings): Boolean;
seems not to be affected by this.
Mantis conversion info:
- Mantis ID: 13307
- OS: WinMe
- OS Build: MS
- Platform: i386
- Version: 2.2.2
- Fixed in version: 2.4.0
- Fixed in revision: 12885 (#8d2f3946)
- Target version: 2.4.0