简体   繁体   English

PHP精度舍入问题

[英]PHP precision rounding question

Wanted to as a simple question. 想要作为一个简单的问题。

Code: 码:

$myarray = array(5.0000);
echo round($myarray[0],2);

Ouput: 输出继电器:

5

Why is the output 5 and not 5.00 ? 为什么输出5而不是5.00?

Thanks 谢谢

It's because round actually rounds off the data you give to it, it doesn't change how that data is printed. 这是因为round实际上是对您提供给它的数据进行四舍五入,不会改变数据的打印方式。

If you want to specify how the data is output, look into using printf , such as: 如果要指定数据的输出方式,请使用printf ,例如:

printf ("%.2f\n", $myarray[0]);

It doesn't show the 0's after the decimal point if there are only 0's. 如果只有0,则小数点后不显示0。

5.050 would be shown as 5.05... 5.050将显示为5.05 ...

you can have a look at the number_format function if you want to show the 0's. 如果要显示0,可以查看number_format函数。

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

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