Delphi - 使用 FormatFloat('0.##',argValue 时浮点舍入不一致)



尝试使用函数FormatFloat(格式化字符串'0.##'(将双精度值四舍五入到小数点后两位。

以下是inputoutput

231.545 -> 231.54 (but expected output is 231.55)
2.315 -> 2.31 (but expected output is 2.32)

23.045 -> 23.05 (gives expected output 23.05)
23.145 -> 23.14 (but expected output 23.15)

23.245 -> 23.25 (gives expected output 23.25)
23.345 -> 23.34 (but expected output 23.35)

23.445 -> 23.45 (gives expected output 23.45)
23.545 -> 23.55 (gives expected output 23.55)
23.645 -> 23.65 (but expected output 23.64)

23.745 -> 23.75 (gives expected output 23.75)
23.845 -> 23.84 (but expected output 23.84)
23.945 -> 23.95 (gives expected output 23.95)

为什么会发生这种奇怪的行为?我使用的是Delphi7。

二进制浮点值不能精确地表示每个值。这就是你所看到的。

例如,值2.315以双倍精度表示为:

2.31499 99999 99999 94670 92948 17992 48605 96656 79931 64062 5

这将四舍五入为2.31


如果您可以使用十进制数据类型,如currency,您可以获得所需的输出(如果货币在您的工作范围内(:

var
c : Currency;
begin
c := 2.315;
WriteLn(FormatFloat('0.##',c)); // Outputs 2.32
end.

另一种选择是使用小数库,如BigDecimals,但这需要一个现代Delphi版本,支持带方法的记录。

最新更新