PHP没有正确地舍入浮动?

时间:2013-01-29 04:33:25

标签: php

我正试图绕过双0.0045。当我打电话给...

echo round($theFloat, 3);

我得到0.004,而不是0.005,这是预期的反应。

以下是代码:

$increase = 1.1;
$previousPrice = round(0.11 / $increase, 2);
$nextPrice = round(0.11 * $increase, 2);
$afterCut = round(0.11 * 0.95, 6);
$willSend = $afterCut - $previousPrice;
echo round($willSend, 3);

2 个答案:

答案 0 :(得分:1)

试试这个

$increase = 1.1;
$previousPrice = 0.11 / $increase;
$nextPrice = 0.11 * $increase;
$afterCut = round(0.11 * 0.95, 6);
$willSend = $afterCut - $previousPrice;
echo round($willSend, 3);

答案 1 :(得分:1)

虽然您拥有的内容,如0.0045,但实际的值约为0.00449999999999999966,因为IEEE 754 floating point values cannot specify exactly that number 值在舍入到3位时为0.004。