$price = 10.00;
list($dollars, $cents) = explode('.', $price);
echo $dollars . '.' . $cents;
Run Code Online (Sandbox Code Playgroud)
...除了省略零之外几乎可以工作.10.00变10和10.10变10.1
我看到有一个字符串的填充函数,但数字或浮点数是什么?
我该如何解决?
你可以使用number_format:
echo number_format($price, 2); // Would print 10.00
Run Code Online (Sandbox Code Playgroud)
您可以为小数点指定分隔符,为数千指定另一个分隔符:
echo number_format(1234.56, 2, ',', ' '); // Would print 1 234,56
Run Code Online (Sandbox Code Playgroud)