打印小数点后3位数的PHP浮点数?

时间:2022-01-15 17:09:20

Is there an easy way to echo a float number with a specific amount of digits after the decimal point?

是否有一种简单的方法来回显小数点后具有特定位数的浮点数?

For example: $sum = 3.1234566768; I would like to echo $sum and get: 3.12.

例如:$ sum = 3.1234566768;我想回应$ sum并得到:3.12。

4 个解决方案

#1


30  

use number_format()

使用number_format()

number_format($sum,2);

#2


3  

echo number_format($sum, 2); // 3.12

#3


2  

Try with:

试试:

$sum = 3.1234566768;
$rounded = round($sum, 2);

#4


0  

echo number_format((float)$ans, 4, '.', '');

I think this will work out

我想这会有用

#1


30  

use number_format()

使用number_format()

number_format($sum,2);

#2


3  

echo number_format($sum, 2); // 3.12

#3


2  

Try with:

试试:

$sum = 3.1234566768;
$rounded = round($sum, 2);

#4


0  

echo number_format((float)$ans, 4, '.', '');

I think this will work out

我想这会有用