$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条件永远不会起作用,否则只能起作用
答案 0 :(得分:1)
一种不是传统的做法:
$parts = explode(".", $x);
$integerPart = number_format($parts[0], 0, '', ',');
$result = $integerPart.".".$parts[1];