简体   繁体   English

在小数点后打印PHP float 2位数?

[英]Printing PHP float with 2 digits after the decimal point?

Is there an easy way to echo a float number with a specific amount of digits after the decimal point? 是否有一种简单的方法echo显小数点后具有特定位数的浮点数?

For example: $sum = 3.1234566768; 例如: $sum = 3.1234566768; I would like to echo $sum and get: 3.12 . 我想回应$ sum并得到: 3.12

使用number_format()

number_format($sum,2);

Try with: 试试:

$sum = 3.1234566768;
$rounded = round($sum, 2);
echo number_format($sum, 2); // 3.12
echo number_format((float)$ans, 4, '.', '');

我认为这会成功

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

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