1
$result = number_format($x, 2, '.', ','); //Will do the following correctly
115255 = 115,225.00
115255.4 = 115,225.40
115255.40 = 115,225.40
115255.455 = 115,255.46

但是当用户在小数点后输入超过 2 位时,我需要,而不是将它们仅切成 2 位小数并按原样使用它......

115255.455 = 115,255.455
115255.4557 = 115,255.4557

我可以做这样的事情吗?

if($x == number_format($x, 3)) //I will do it in while loop later, lets test 3 now
    $result = number_format($x, 3, '.', ',');
else $result = number_format($x, 2, '.', ',');

前面的 if 条件永远不会起作用,否则只会起作用

4

1 回答 1

1

一种不传统的方法:

$parts = explode(".", $x);
$integerPart = number_format($parts[0], 0, '', ',');
$result = $integerPart.".".$parts[1];
于 2013-11-09T05:07:50.197 回答