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