diff --git a/components/fpspreadsheet/source/common/fpsnumformat.pas b/components/fpspreadsheet/source/common/fpsnumformat.pas index c5b228b37..020beb095 100644 --- a/components/fpspreadsheet/source/common/fpsnumformat.pas +++ b/components/fpspreadsheet/source/common/fpsnumformat.pas @@ -900,6 +900,22 @@ end; -------------------------------------------------------------------------------} function ConvertFloatToStr(AValue: Double; AParams: TsNumFormatParams; AFormatSettings: TFormatSettings): String; + + { Returns true if s represent the value 0; it can be written in various + ways: '0', '0.00', '0,000.0', '0.00E+10' etc. } + function IsZeroStr(s: String): Boolean; + var + i: Integer; + begin + Result := false; + for i:=1 to Length(s) do + case s[i] of + 'e', 'E': break; + '1'..'9': exit; + end; + Result := true; + end; + var fs: TFormatSettings absolute AFormatSettings; sidx: Integer; @@ -961,7 +977,7 @@ begin else // Floating-point or integer s := ProcessFloatFormat(AValue, fs, section.Elements, el); - if (sidx = 0) and isNeg then s := '-' + s; + if (sidx = 0) and isNeg and not IsZeroStr(s) then s := '-' + s; Result := Result + s; Continue; end diff --git a/components/fpspreadsheet/tests/formattests.pas b/components/fpspreadsheet/tests/formattests.pas index 67b75f8c8..83fcf9369 100644 --- a/components/fpspreadsheet/tests/formattests.pas +++ b/components/fpspreadsheet/tests/formattests.pas @@ -225,8 +225,11 @@ begin SollNumberStrings[i, 0] := FloatToStr(SollNumbers[i], fs); SollNumberStrings[i, 1] := FormatFloat('0', SollNumbers[i], fs); SollNumberStrings[i, 2] := FormatFloat('0.00', SollNumbers[i], fs); - SollNumberStrings[i, 3] := FormatFloat('#,##0', SollNumbers[i], fs); - SollNumberStrings[i, 4] := FormatFloat('#,##0.00', SollNumbers[i], fs); + // For the next two cases don't use FormatFloat('#,##0') and + // FormatFloat('#,##0.00') which produce '-0' and '-0.00', respectively, + // for the case of -1.23456E-6 which is not consistent with Excel. + SollNumberStrings[i, 3] := Format('%.0n', [SollNumbers[i]], fs); + SollNumberStrings[i, 4] := Format('%.2n', [SollNumbers[i]], fs); SollNumberStrings[i, 5] := FormatFloat('0.00E+00', SollNumbers[i], fs); SollNumberStrings[i, 6] := FormatFloat('0', SollNumbers[i]*100, fs) + '%'; SollNumberStrings[i, 7] := FormatFloat('0.00', SollNumbers[i]*100, fs) + '%';