2

PHP 版本 5.3.6

例子

<?php
$timeStart = microtime(true);
// some code ...    
$timeExecution = microtime(true) - $timeStart;
$time = round($timeExecution, 2);
file_put_contents('h:/round.txt', $timeExecution . ' = ' . $time . "\n", FILE_APPEND);
?>

文件 round.txt 包含以下几行:

131.3048491477966 = 131.3

8.340715885162354 = 8.34

8.198318004608154 = 8.199999999999999

这怎么可能?

4

1 回答 1

3

http://php.net/manual/en/language.types.float.php

此外,可以精确表示为以 10 为底的浮点数的有理数,如 0.1 或 0.7,没有精确表示为以 2 为底的浮点数,无论尾数大小如何,它都在内部使用。因此,它们不能被转换成它们内部的二进制对应物,而不会有小的精度损失。这可能会导致令人困惑的结果:例如, floor((0.1+0.7)*10) 通常会返回 7 而不是预期的 8,因为内部表示将类似于 7.9999999999999991118 ...。

于 2012-01-20T19:59:48.320 回答