简体   繁体   English

PHP十进制舍入错误

[英]PHP decimal rounding error

$duration = 35780.123000; // duration in seconds.

$milliseconds = $duration - floor($duration);
echo $milliseconds
// 0.12299999999959

How do I extract the milliseconds 0.123000 without freaky rounding errors? 如何提取毫秒数0.123000而不会出现怪异的舍入错误? I need to keep 6 decimals for precision. 我需要保留6位小数以保持精度。 (So not 0.123) (所以不是0.123)

尝试这个:

number_format($number, 6)
echo number_format($milliseconds,6);

http://php.net/manual/zh/function.number-format.php

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM