gpt4 book ai didi

php - 显示一个数字到小数点后两位

转载 作者:bug小助手 更新时间:2023-10-28 10:41:47 25 4
gpt4 key购买 nike

将 PHP 字符串四舍五入到小数点后两位的正确方法是什么?

$number = "520"; // It's a string from a database

$formatted_number = round_to_2dp($number);

echo $formatted_number;

输出应该是520.00;

round_to_2dp()函数定义应该如何?

最佳答案

您可以使用 number_format() :

return number_format((float)$number, 2, '.', '');

例子:

$foo = "105";
echo number_format((float)$foo, 2, '.', ''); // Outputs -> 105.00

这个函数返回一个字符串

关于php - 显示一个数字到小数点后两位,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4483540/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com