mirror of
https://gitlab.com/freepascal.org/fpc/source.git
synced 2025-04-13 14:49:47 +02:00
* fixed TextToFloat() in case ThousandSeparator = DecimalSeparator, based
on patch by Bart Broersma (mantis #13307) git-svn-id: trunk@12885 -
This commit is contained in:
parent
78c12ba82b
commit
8d2f3946e0
1
.gitattributes
vendored
1
.gitattributes
vendored
@ -8795,6 +8795,7 @@ tests/webtbs/tw13187.pp svneol=native#text/plain
|
||||
tests/webtbs/tw13210.pp svneol=native#text/plain
|
||||
tests/webtbs/tw1323.pp svneol=native#text/plain
|
||||
tests/webtbs/tw1327.pp svneol=native#text/plain
|
||||
tests/webtbs/tw13307.pp svneol=native#text/plain
|
||||
tests/webtbs/tw1331.pp svneol=native#text/plain
|
||||
tests/webtbs/tw13313.pp svneol=native#text/plain
|
||||
tests/webtbs/tw13313a.pp svneol=native#text/plain
|
||||
|
@ -1046,23 +1046,27 @@ Var
|
||||
|
||||
Begin
|
||||
S:=StrPas(Buffer);
|
||||
if (FormatSettings.DecimalSeparator<>'.') then
|
||||
//ThousandSeparator not allowed as by Delphi specs
|
||||
if (FormatSettings.ThousandSeparator <> FormatSettings.DecimalSeparator) and
|
||||
(Pos(FormatSettings.ThousandSeparator, S) <> 0) then
|
||||
begin
|
||||
{ only decimalseparator may appear in the string }
|
||||
P:=Pos('.',S);
|
||||
if (P<>0) then
|
||||
begin
|
||||
result:=false;
|
||||
exit;
|
||||
end;
|
||||
P:=Pos(FormatSettings.DecimalSeparator,S);
|
||||
If (P<>0) Then
|
||||
S[P] := '.';
|
||||
Result := False;
|
||||
Exit;
|
||||
end;
|
||||
if (FormatSettings.DecimalSeparator <> '.') and
|
||||
(Pos('.', S) <>0) then
|
||||
begin
|
||||
Result := False;
|
||||
Exit;
|
||||
end;
|
||||
P:=Pos(FormatSettings.DecimalSeparator,S);
|
||||
If (P<>0) Then
|
||||
S[P] := '.';
|
||||
Val(trim(S),Value,E);
|
||||
Result:=(E=0);
|
||||
End;
|
||||
|
||||
|
||||
Function TextToFloat(Buffer: PChar; Out Value: Extended): Boolean;
|
||||
|
||||
begin
|
||||
@ -1086,12 +1090,20 @@ Var
|
||||
|
||||
Begin
|
||||
S:=StrPas(Buffer);
|
||||
P:=Pos(FormatSettings.ThousandSeparator,S);
|
||||
While (P<>0) do
|
||||
//ThousandSeparator not allowed as by Delphi specs
|
||||
if (FormatSettings.ThousandSeparator <> FormatSettings.DecimalSeparator) and
|
||||
(Pos(FormatSettings.ThousandSeparator, S) <> 0) then
|
||||
begin
|
||||
Delete(S,P,1);
|
||||
P:=Pos(FormatSettings.ThousandSeparator,S);
|
||||
Result := False;
|
||||
Exit;
|
||||
end;
|
||||
if (FormatSettings.DecimalSeparator <> '.') and
|
||||
(Pos('.', S) <>0) then
|
||||
begin
|
||||
Result := False;
|
||||
Exit;
|
||||
end;
|
||||
|
||||
P:=Pos(FormatSettings.DecimalSeparator,S);
|
||||
If (P<>0) Then
|
||||
S[P] := '.';
|
||||
@ -1120,6 +1132,7 @@ Begin
|
||||
Result:=(E=0);
|
||||
End;
|
||||
|
||||
|
||||
Function TryStrToFloat(Const S : String; Out Value: Single): Boolean;
|
||||
|
||||
begin
|
||||
|
155
tests/webtbs/tw13307.pp
Normal file
155
tests/webtbs/tw13307.pp
Normal file
@ -0,0 +1,155 @@
|
||||
program stf2;
|
||||
|
||||
{$IFDEF FPC}
|
||||
{$mode objfpc}{$H+}
|
||||
{$ENDIF}
|
||||
|
||||
uses SysUtils;
|
||||
|
||||
|
||||
var S: String;
|
||||
E: Extended;
|
||||
B: Boolean;
|
||||
Code: Integer;
|
||||
begin
|
||||
DecimalSeparator := '.';
|
||||
ThousandSeparator := ',';
|
||||
writeln('DecimalSeparator = ',DecimalSeparator);
|
||||
writeln('ThousandSeparator = ',ThousandSeparator);
|
||||
E := -1.0;
|
||||
S := '123.456';
|
||||
B := TextToFloat(PChar(S), E, fvExtended, DefaultFormatSettings);
|
||||
if B then writeln(Format('(1) "%s" -> %.3f',[S,E]))
|
||||
else
|
||||
begin
|
||||
writeln(Format('(1) "%s" -> Conversion Error',[S]));
|
||||
halt(1);
|
||||
end;
|
||||
B := TextToFloat(PChar(S), E, DefaultFormatSettings);
|
||||
if B then writeln(Format('(2) "%s" -> %.3f',[S,E]))
|
||||
else
|
||||
begin
|
||||
writeln(Format('(2) "%s" -> Conversion Error',[S]));
|
||||
halt(1);
|
||||
end;
|
||||
S := '123,456';
|
||||
B := TextToFloat(PChar(S), E, fvExtended, DefaultFormatSettings);
|
||||
if B then writeln(Format('(1) "%s" -> %.3f',[S,E]))
|
||||
else writeln(Format('(1) "%s" -> Conversion Error',[S]));
|
||||
B := TextToFloat(PChar(S), E, DefaultFormatSettings);
|
||||
if B then writeln(Format('(2) "%s" -> %.3f',[S,E]))
|
||||
else writeln(Format('(2) "%s" -> Conversion Error',[S]));
|
||||
|
||||
|
||||
|
||||
DecimalSeparator := ',';
|
||||
ThousandSeparator := '.';
|
||||
writeln('DecimalSeparator = ',DecimalSeparator);
|
||||
writeln('ThousandSeparator = ',ThousandSeparator);
|
||||
E := -1.0;
|
||||
S := '123.456';
|
||||
B := TextToFloat(PChar(S), E, fvExtended, DefaultFormatSettings);
|
||||
if B then
|
||||
begin
|
||||
writeln(Format('(1) "%s" -> %.3f',[S,E]));
|
||||
halt(1);
|
||||
end
|
||||
else writeln(Format('(1) "%s" -> Conversion Error',[S]));
|
||||
B := TextToFloat(PChar(S), E, DefaultFormatSettings);
|
||||
if B then
|
||||
begin
|
||||
writeln(Format('(2) "%s" -> %.3f',[S,E]));
|
||||
halt(1);
|
||||
end
|
||||
else writeln(Format('(2) "%s" -> Conversion Error',[S]));
|
||||
S := '123,456';
|
||||
B := TextToFloat(PChar(S), E, fvExtended, DefaultFormatSettings);
|
||||
if B then writeln(Format('(1) "%s" -> %.3f',[S,E]))
|
||||
else
|
||||
begin
|
||||
writeln(Format('(1) "%s" -> Conversion Error',[S]));
|
||||
halt(1);
|
||||
end;
|
||||
B := TextToFloat(PChar(S), E, DefaultFormatSettings);
|
||||
if B then writeln(Format('(2) "%s" -> %.3f',[S,E]))
|
||||
else
|
||||
begin
|
||||
writeln(Format('(2) "%s" -> Conversion Error',[S]));
|
||||
halt(1);
|
||||
end;
|
||||
|
||||
DecimalSeparator := ',';
|
||||
ThousandSeparator := ',';
|
||||
writeln('DecimalSeparator = ',DecimalSeparator);
|
||||
writeln('ThousandSeparator = ',ThousandSeparator);
|
||||
E := -1.0;
|
||||
S := '123.456';
|
||||
B := TextToFloat(PChar(S), E, fvExtended, DefaultFormatSettings);
|
||||
if B then
|
||||
begin
|
||||
writeln(Format('(1) "%s" -> %.3f',[S,E]));
|
||||
halt(1);
|
||||
end
|
||||
else writeln(Format('(1) "%s" -> Conversion Error',[S]));
|
||||
B := TextToFloat(PChar(S), E, DefaultFormatSettings);
|
||||
if B then
|
||||
begin
|
||||
writeln(Format('(12 "%s" -> %.3f',[S,E]));
|
||||
halt(1);
|
||||
end
|
||||
else writeln(Format('(2) "%s" -> Conversion Error',[S]));
|
||||
S := '123,456';
|
||||
B := TextToFloat(PChar(S), E, fvExtended, DefaultFormatSettings);
|
||||
if B then writeln(Format('(1) "%s" -> %.3f',[S,E]))
|
||||
else
|
||||
begin
|
||||
writeln(Format('(1) "%s" -> Conversion Error',[S]));
|
||||
halt(1);
|
||||
end;
|
||||
B := TextToFloat(PChar(S), E, DefaultFormatSettings);
|
||||
if B then writeln(Format('(2) "%s" -> %.3f',[S,E]))
|
||||
else
|
||||
begin
|
||||
writeln(Format('(2) "%s" -> Conversion Error',[S]));
|
||||
halt(1);
|
||||
end;
|
||||
|
||||
DecimalSeparator := '.';
|
||||
ThousandSeparator := '.';
|
||||
writeln('DecimalSeparator = ',DecimalSeparator);
|
||||
writeln('ThousandSeparator = ',ThousandSeparator);
|
||||
E := -1.0;
|
||||
S := '123.456';
|
||||
B := TextToFloat(PChar(S), E, fvExtended, DefaultFormatSettings);
|
||||
if B then writeln(Format('(1) "%s" -> %.3f',[S,E]))
|
||||
else
|
||||
begin
|
||||
writeln(Format('(1) "%s" -> Conversion Error',[S]));
|
||||
halt(1);
|
||||
end;
|
||||
B := TextToFloat(PChar(S), E, DefaultFormatSettings);
|
||||
if B then writeln(Format('(2) "%s" -> %.3f',[S,E]))
|
||||
else
|
||||
begin
|
||||
writeln(Format('(2) "%s" -> Conversion Error',[S]));
|
||||
halt(1);
|
||||
end;
|
||||
S := '123,456';
|
||||
B := TextToFloat(PChar(S), E, fvExtended, DefaultFormatSettings);
|
||||
if B then
|
||||
begin
|
||||
writeln(Format('(1) "%s" -> %.3f',[S,E]));
|
||||
halt(1);
|
||||
end
|
||||
else writeln(Format('(1) "%s" -> Conversion Error',[S]));
|
||||
B := TextToFloat(PChar(S), E, DefaultFormatSettings);
|
||||
if B then
|
||||
begin
|
||||
writeln(Format('(2) "%s" -> %.3f',[S,E]));
|
||||
halt(1);
|
||||
end
|
||||
else writeln(Format('(2) "%s" -> Conversion Error',[S]));
|
||||
|
||||
|
||||
end.
|
||||
|
Loading…
Reference in New Issue
Block a user