请帮我解决php中的数字格式 例如 我有一些计算,比如a + b = c我想在php中以999.99格式回答我已经读过number_format,sprintf但它没用。请给我一些有用的想法。我的最终结果应该是999.99格式。
答案 0 :(得分:11)
根据“无用”文档:
$num = 999.98353;
$num = number_format($num, 2, '.', '');
如果你花时间去实际read the documentation,那就有相关的例子。
答案 1 :(得分:3)
除了number_format,您还可以使用sprintf
$a = 999;
$b = .99;
$c = $a + $b;
echo sprintf('%03.2f', $c); // 999.99
答案 2 :(得分:1)
$num = 999.98353;
$num1 = 10;
$num2 = $num + $num1;
echo number_format($num2, 2, '.', ''); // output 1009.98
答案 3 :(得分:1)
试试这个
$a = 500.3755;
$b = 600.9855;
$c = $a + $b;
echo number_format($c, 2, '.', ''); //1101.36
echo number_format($c, 2, '.', ','); //1,101.36
答案 4 :(得分:1)
您可以使用
$c = sprintf('%0.2f',$a)+sprintf('%0.2f',$b);
或者您也可以尝试:
$c = $a+ $b; // If $c = 999.9999
$c = substr($c, 0, 2); // $c is now the string 999.9999
echo( number_format($c-1,2) ); // Will output 998.99
答案 5 :(得分:1)
$a = 1234.5678; $b = 1234.5678; $num = round(($a + $b), 2); echo number_format($num, 2, '.', ',');
number_format有效,请尝试发布一些代码。