我在格式化十进制数时遇到问题,假设我可以有两个十进制数,其中十进制可以在第18位之前或之后,格式如下:
$num1 = 12345678912345678912345.52458
$num2 = 123.54893256666666
对于上面两个数字我想要的是基于条件
for $num1: 如果小数位于第18位之后,那么小数应该在18位之后,之后2位仅作为精度
for $num2: 如果小数位于第18位之前,那么小数应该是位置,但精度应该只是小数点后的2位数
期望:
$num1 = 123456789123456789.00
$num2 = 123.54
我试过这个号码 number_format()
如下:
<?php
$num1 = 12345678912345678912345.52458;
$num2 = 123.54893256666666;
$num1 = number_format($num1,2,'.',''); //it is not working
$num2 = number_format($num2,2,'.',''); //it is working
echo $num1."<br>".$num2;
输出:
for $num1 = 12345678912345679593472.00 where it should comes like 123456789123456789.00
for $num1 = 123.55 that is ok
请帮助我没有得到如何使用 number_format()
为$ num1
1 回答
为什么你需要这么多?默认情况下,php支持14位数 . 例如
如果您编写该代码,它将回显错误,但是如果您向$ num添加一个数字,那么php将无法正确处理它 .
小数点后两位数 . 看到此链接已经回答了
PHP: show a number to 2 decimal places