简体   繁体   English

PHP中的尾随零

[英]Trailing zeroes in PHP

I have a value $rating that is between 0.00 and 10.00. 我的值$rating在0.00和10.00之间。

I need to add trailing zeroes to numbers like 3.70 (which is 3.7) and 5.00 (which is 5). 我需要将尾随零添加到3.70(即3.7)和5.00(即5)之类的数字上。 Do I need some kind of if statement too to check what value $rating is and then do some kind of str_pad ? 我是否还需要某种if语句来检查$rating值,然后执行某种str_pad I'm not sure what to do. 我不确定该怎么办。

See the number_format function 参见number_format函数

echo number_format(5, 2);    // returns 5.00
echo number_format(5.2, 2);  // returns 5.20
echo number_format(5.24, 2); // returns 5.24

尝试使用number_format,如下所示:

number_format((float)$yourNumber, 2, '.', '');

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

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