显示一个数字到2位小数 [英] Show a number to 2 decimal places
本文介绍了显示一个数字到2位小数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
$ number =520;将PHP字符串舍入到小数点后两位的正确方法是什么? //这是一个DB
$ b $ 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,'。',''); //输出 - > 105.00
该函数返回一个字符串
。What's the correct way to round a PHP string to 2 decimal places?
$number = "520"; // It's a string from a DB
$formatted_number = round_to_2dp($number);
echo $formatted_number;
The output should be 520.00
;
How should the round_to_2dp()
function definition be?
解决方案
You can use number_format():
return number_format((float)$number, 2, '.', '');
Example:
$foo = "105";
echo number_format((float)$foo, 2, '.', ''); // Outputs -> 105.00
This function returns a string.
这篇关于显示一个数字到2位小数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文