Home >Backend Development >PHP Tutorial >价格格式化问题。
首先感谢这个 CSDN 平台,帮助我们解决了很多难道,非常感谢!
现在我有一个价格格式化问题,求大家指点,谢谢。
例如 100000 自动格式化为 10万
56000 自动格式化为 5.6万
6000 就显示 6000
虽然有些难题还是解决不了,但也同样会感谢 CSDN 平台,非常感谢!
自己写一个方法如果是五位整数除以一万如果不是直接显示
header("Content-type: text/html; charset=utf-8");function removeAfterZero($str) { $s = $str; $r = ''; for ($i = strlen($s)-1; $i > 0; $i--) { if ($s[$i] == 0) { $s = substr($s, 0, $i); $r = $s; } else { break; } } return $r;}$num = 1000100;if (strlen(strlen($num) < 5)) echo $num;else if (strlen($num) >= 5 && strlen($num) < 6) { if (removeAfterZero(substr($num, 1, strlen($num)-1)) == 0) echo substr($num, 0, 1) . "万"; else echo substr($num, 0, 1) . '.' . removeAfterZero(substr($num, 1, strlen($num)-1)) . "万";}else if (strlen($num) >= 6 && strlen($num) < 7) { if (removeAfterZero(substr($num, 2, strlen($num)-2)) == 0) echo substr($num, 0, 2) . "万"; else echo substr($num, 0, 2) . '.' . removeAfterZero(substr($num, 2, strlen($num)-2)) . "万";}else if (strlen($num) >= 7 && strlen($num) < 8) { if (removeAfterZero(substr($num, 3, strlen($num)-3)) == 0) echo substr($num, 0, 3) . "万"; else echo substr($num, 0, 3) . '.' . removeAfterZero(substr($num, 3, strlen($num)-3)) . "万";}
$num = 100000;echo $num < 10000 ? $num : $num/10000 . '万';
$num = 100000;echo $num < 10000 ? $num : sprintf("%0.1f",$num/10000) . '万';
$num = 100000;echo $num < 10000 ? $num : $num/10000 . '万';
$num = 110000001;echo price_format($num);function price_format($num){ $p=rtrim(number_format($num < 1e4 ? $num :$num<1e8? $num/1e4 : $num/1e8,2 ),'0'); return $num < 1e4 ? $num :$num<1e8? ($p . '万'): ($p . '亿');}
$num = 100000;echo $num < 10000 ? $num : $num/10000 . '万';