您可以使用number_format() http://php.net/number_format为了达成这个:
echo number_format((float) $number, $precision, '.', '');
这将转换1518845.756789
to 1518845.757
.
但如果你只是想把小数位数截掉到3位,并且not round,那么您可以执行以下操作:
$number = intval($number * ($p = pow(10, $precision))) / $p;
It may look intimidating at first, but the concept is really simple. You have a number, you multiply it by 103 (it becomes 1518845756.789
), cast it to an integer so everything after the 3 decimal places is removed (becomes 1518845756
), and then divide the result by 103 (becomes 1518845.756
).
Demo https://eval.in/174957