mirror of
https://gitlab.com/freepascal.org/fpc/source.git
synced 2025-08-17 19:49:22 +02:00
* fixed FloatToStrF for ffExponent format.
* fixed FloatToStrF for non Extended values. + added test for FloatToStr. git-svn-id: trunk@5894 -
This commit is contained in:
parent
3381fd2ea1
commit
a4a125e561
1
.gitattributes
vendored
1
.gitattributes
vendored
@ -6896,6 +6896,7 @@ tests/test/units/sysutils/execansi.pp svneol=native#text/plain
|
|||||||
tests/test/units/sysutils/execedbya.pp svneol=native#text/plain
|
tests/test/units/sysutils/execedbya.pp svneol=native#text/plain
|
||||||
tests/test/units/sysutils/extractquote.pp svneol=native#text/plain
|
tests/test/units/sysutils/extractquote.pp svneol=native#text/plain
|
||||||
tests/test/units/sysutils/tfile1.pp svneol=native#text/plain
|
tests/test/units/sysutils/tfile1.pp svneol=native#text/plain
|
||||||
|
tests/test/units/sysutils/tfloattostr.pp -text
|
||||||
tests/test/units/sysutils/tsscanf.pp svneol=native#text/plain
|
tests/test/units/sysutils/tsscanf.pp svneol=native#text/plain
|
||||||
tests/test/units/sysutils/tstrtobool.pp svneol=native#text/plain
|
tests/test/units/sysutils/tstrtobool.pp svneol=native#text/plain
|
||||||
tests/test/uprec6.pp svneol=native#text/plain
|
tests/test/uprec6.pp svneol=native#text/plain
|
||||||
|
@ -1030,8 +1030,6 @@ Function FloatToStrFIntl(const Value; format: TFloatFormat; Precision, Digits: I
|
|||||||
Var
|
Var
|
||||||
P: Integer;
|
P: Integer;
|
||||||
Negative, TooSmall, TooLarge: Boolean;
|
Negative, TooSmall, TooLarge: Boolean;
|
||||||
ValExt: Extended;
|
|
||||||
ValCur: Currency;
|
|
||||||
|
|
||||||
Begin
|
Begin
|
||||||
Case format Of
|
Case format Of
|
||||||
@ -1055,9 +1053,9 @@ Begin
|
|||||||
Begin
|
Begin
|
||||||
case ValueType of
|
case ValueType of
|
||||||
fvDouble:
|
fvDouble:
|
||||||
Str(Double(Value):0:precision, Result);
|
Str(Double(Extended(Value)):0:precision, Result);
|
||||||
fvSingle:
|
fvSingle:
|
||||||
Str(Single(Value):0:precision, Result);
|
Str(Single(Extended(Value)):0:precision, Result);
|
||||||
fvCurrency:
|
fvCurrency:
|
||||||
{$ifdef FPC_HAS_STR_CURRENCY}
|
{$ifdef FPC_HAS_STR_CURRENCY}
|
||||||
Str(Currency(Value):0:precision, Result);
|
Str(Currency(Value):0:precision, Result);
|
||||||
@ -1124,30 +1122,43 @@ Begin
|
|||||||
If (Precision = -1) Or (Precision > maxdigits) Then Precision := maxdigits;
|
If (Precision = -1) Or (Precision > maxdigits) Then Precision := maxdigits;
|
||||||
case ValueType of
|
case ValueType of
|
||||||
fvDouble:
|
fvDouble:
|
||||||
Str(Double(Value):Precision+8, Result);
|
Str(Double(Extended(Value)):Precision+7, Result);
|
||||||
fvSingle:
|
fvSingle:
|
||||||
Str(Single(Value):Precision+8, Result);
|
Str(Single(Extended(Value)):Precision+6, Result);
|
||||||
fvCurrency:
|
fvCurrency:
|
||||||
{$ifdef FPC_HAS_STR_CURRENCY}
|
{$ifdef FPC_HAS_STR_CURRENCY}
|
||||||
Str(Currency(Value):Precision+8, Result);
|
Str(Currency(Value):Precision+6, Result);
|
||||||
{$else}
|
{$else}
|
||||||
Str(Extended(Currency(Value)):Precision+8, Result);
|
Str(Extended(Currency(Value)):Precision+8, Result);
|
||||||
{$endif FPC_HAS_STR_CURRENCY}
|
{$endif FPC_HAS_STR_CURRENCY}
|
||||||
else
|
else
|
||||||
Str(Extended(Value):Precision+8, Result);
|
Str(Extended(Value):Precision+8, Result);
|
||||||
end;
|
end;
|
||||||
Result[3] := DecimalSeparator;
|
{ Delete leading spaces }
|
||||||
P:=4;
|
while Result[1] = ' ' do
|
||||||
While (P>0) and (Digits < P) And (Result[Precision + 5] = '0') do
|
|
||||||
Begin
|
|
||||||
If P<>1 then
|
|
||||||
system.Delete(Result, Precision + 5, 1)
|
|
||||||
else
|
|
||||||
system.Delete(Result, Precision + 3, 3);
|
|
||||||
Dec(P);
|
|
||||||
end;
|
|
||||||
If Result[1] = ' ' Then
|
|
||||||
System.Delete(Result, 1, 1);
|
System.Delete(Result, 1, 1);
|
||||||
|
if Result[1] = '-' then
|
||||||
|
Result[3] := DecimalSeparator
|
||||||
|
else
|
||||||
|
Result[2] := DecimalSeparator;
|
||||||
|
if Digits < 4 then
|
||||||
|
begin
|
||||||
|
P:=Pos('E',Result);
|
||||||
|
if P <> 0 then
|
||||||
|
begin
|
||||||
|
Inc(P, 2);
|
||||||
|
while (Result[P] = '0') and (Digits < 4) do
|
||||||
|
begin
|
||||||
|
System.Delete(Result, P, 1);
|
||||||
|
if P > Length(Result) then
|
||||||
|
begin
|
||||||
|
System.Delete(Result, P - 2, 2);
|
||||||
|
break;
|
||||||
|
end;
|
||||||
|
Inc(Digits);
|
||||||
|
end;
|
||||||
|
end;
|
||||||
|
end;
|
||||||
End;
|
End;
|
||||||
|
|
||||||
ffFixed:
|
ffFixed:
|
||||||
@ -1157,9 +1168,9 @@ Begin
|
|||||||
Else If Digits > 18 Then Digits := 18;
|
Else If Digits > 18 Then Digits := 18;
|
||||||
case ValueType of
|
case ValueType of
|
||||||
fvDouble:
|
fvDouble:
|
||||||
Str(Double(Value):0:Digits, Result);
|
Str(Double(Extended(Value)):0:Digits, Result);
|
||||||
fvSingle:
|
fvSingle:
|
||||||
Str(Single(Value):0:Digits, Result);
|
Str(Single(Extended(Value)):0:Digits, Result);
|
||||||
fvCurrency:
|
fvCurrency:
|
||||||
{$ifdef FPC_HAS_STR_CURRENCY}
|
{$ifdef FPC_HAS_STR_CURRENCY}
|
||||||
Str(Currency(Value):0:Digits, Result);
|
Str(Currency(Value):0:Digits, Result);
|
||||||
@ -1182,9 +1193,9 @@ Begin
|
|||||||
Else If Digits > maxdigits Then Digits := maxdigits;
|
Else If Digits > maxdigits Then Digits := maxdigits;
|
||||||
case ValueType of
|
case ValueType of
|
||||||
fvDouble:
|
fvDouble:
|
||||||
Str(Double(Value):0:Digits, Result);
|
Str(Double(Extended(Value)):0:Digits, Result);
|
||||||
fvSingle:
|
fvSingle:
|
||||||
Str(Single(Value):0:Digits, Result);
|
Str(Single(Extended(Value)):0:Digits, Result);
|
||||||
fvCurrency:
|
fvCurrency:
|
||||||
{$ifdef FPC_HAS_STR_CURRENCY}
|
{$ifdef FPC_HAS_STR_CURRENCY}
|
||||||
Str(Currency(Value):0:Digits, Result);
|
Str(Currency(Value):0:Digits, Result);
|
||||||
@ -1211,45 +1222,25 @@ Begin
|
|||||||
ffCurrency:
|
ffCurrency:
|
||||||
|
|
||||||
Begin
|
Begin
|
||||||
if ValueType = fvCurrency then
|
|
||||||
begin
|
|
||||||
ValCur:=Currency(Value);
|
|
||||||
If ValCur < 0 Then
|
|
||||||
Begin
|
|
||||||
Negative := True;
|
|
||||||
ValCur := -ValCur;
|
|
||||||
End
|
|
||||||
Else Negative := False;
|
|
||||||
end
|
|
||||||
else
|
|
||||||
begin
|
|
||||||
ValExt:=Extended(Value);
|
|
||||||
If ValExt < 0 Then
|
|
||||||
Begin
|
|
||||||
Negative := True;
|
|
||||||
ValExt := -ValExt;
|
|
||||||
End
|
|
||||||
Else Negative := False;
|
|
||||||
end;
|
|
||||||
|
|
||||||
If Digits = -1 Then Digits := CurrencyDecimals
|
If Digits = -1 Then Digits := CurrencyDecimals
|
||||||
Else If Digits > 18 Then Digits := 18;
|
Else If Digits > 18 Then Digits := 18;
|
||||||
case ValueType of
|
case ValueType of
|
||||||
fvDouble:
|
fvDouble:
|
||||||
Str(Double(ValExt):0:Digits, Result);
|
Str(Double(Extended(Value)):0:Digits, Result);
|
||||||
fvSingle:
|
fvSingle:
|
||||||
Str(Single(ValExt):0:Digits, Result);
|
Str(Single(Extended(Value)):0:Digits, Result);
|
||||||
fvCurrency:
|
fvCurrency:
|
||||||
{$ifdef FPC_HAS_STR_CURRENCY}
|
{$ifdef FPC_HAS_STR_CURRENCY}
|
||||||
Str(ValCur:0:Digits, Result);
|
Str(Currency(Value):0:Digits, Result);
|
||||||
{$else}
|
{$else}
|
||||||
Str(Extended(ValCur):0:Digits, Result);
|
Str(Extended(Currency(Value)):0:Digits, Result);
|
||||||
{$endif FPC_HAS_STR_CURRENCY}
|
{$endif FPC_HAS_STR_CURRENCY}
|
||||||
else
|
else
|
||||||
Str(Extended(ValExt):0:Digits, Result);
|
Str(Extended(Value):0:Digits, Result);
|
||||||
end;
|
end;
|
||||||
writeln(result);
|
Negative:=Result[1] = '-';
|
||||||
If Result[1] = ' ' Then System.Delete(Result, 1, 1);
|
if Negative then
|
||||||
|
System.Delete(Result, 1, 1);
|
||||||
P := Pos('.', Result);
|
P := Pos('.', Result);
|
||||||
If P <> 0 Then Result[P] := DecimalSeparator;
|
If P <> 0 Then Result[P] := DecimalSeparator;
|
||||||
Dec(P, 3);
|
Dec(P, 3);
|
||||||
|
52
tests/test/units/sysutils/tfloattostr.pp
Normal file
52
tests/test/units/sysutils/tfloattostr.pp
Normal file
@ -0,0 +1,52 @@
|
|||||||
|
{ Test for FloatToStr and CurrToStr functions. }
|
||||||
|
|
||||||
|
uses sysutils;
|
||||||
|
|
||||||
|
const
|
||||||
|
MaxCurrency : currency = 922337203685477.5807;
|
||||||
|
MinCurrency : currency = -922337203685477.5807;
|
||||||
|
|
||||||
|
var
|
||||||
|
ErrCount: longint;
|
||||||
|
|
||||||
|
procedure CheckResult(const s, ref: string);
|
||||||
|
var
|
||||||
|
ref2: string;
|
||||||
|
begin
|
||||||
|
ref2:=StringReplace(ref, '.', DecimalSeparator, []);
|
||||||
|
if s <> ref2 then
|
||||||
|
begin
|
||||||
|
writeln('Got : ', s);
|
||||||
|
writeln('Should be: ', ref2);
|
||||||
|
Inc(ErrCount);
|
||||||
|
end;
|
||||||
|
end;
|
||||||
|
|
||||||
|
var
|
||||||
|
d: double;
|
||||||
|
s: single;
|
||||||
|
c: currency;
|
||||||
|
begin
|
||||||
|
d:=12345.12345;
|
||||||
|
s:=12345.12;
|
||||||
|
c:=12345.1234;
|
||||||
|
CheckResult(FloatToStrF(d,ffExponent,11,0), '1.2345123450E+4');
|
||||||
|
CheckResult(FloatToStrF(s,ffExponent,8,0), '1.2345120E+4');
|
||||||
|
CheckResult(FloatToStrF(c,ffExponent,10,0), '1.234512340E+4');
|
||||||
|
CheckResult(FloatToStrF(-12345.12345,ffExponent,11,0), '-1.2345123450E+4');
|
||||||
|
CheckResult(FloatToStrF(-0.00000123,ffGeneral,15,0), '-1.23E-6');
|
||||||
|
CheckResult(FloatToStrF(-12345.12345,ffGeneral,7,0), '-12345.12');
|
||||||
|
CheckResult(CurrToStr(-12345.1234), '-12345.1234');
|
||||||
|
CheckResult(CurrToStr(MaxCurrency), '922337203685477.5807');
|
||||||
|
CheckResult(CurrToStr(MinCurrency), '-922337203685477.5807');
|
||||||
|
NegCurrFormat:=8;
|
||||||
|
CheckResult(FloatToStrF(-12345.1234,ffCurrency,19,4), '-12' + ThousandSeparator + '345.1234 ' + CurrencyString);
|
||||||
|
CheckResult(FloatToStrF(MinCurrency,ffCurrency,19,4), '-922' + ThousandSeparator + '337' + ThousandSeparator + '203' + ThousandSeparator + '685' + ThousandSeparator + '477.5807 ' + CurrencyString);
|
||||||
|
if ErrCount > 0 then
|
||||||
|
begin
|
||||||
|
writeln('Test failed. Errors: ', ErrCount);
|
||||||
|
Halt(1);
|
||||||
|
end
|
||||||
|
else
|
||||||
|
writeln('Test completed.');
|
||||||
|
end.
|
Loading…
Reference in New Issue
Block a user