价格格式化问题。

WBOY
Release: 2016-06-23 13:43:00
Original
819 people have browsed it

首先感谢这个 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)) . "万";}
Copy after login

$num = 100000;echo $num < 10000 ? $num : $num/10000 . '万';
Copy after login
Copy after login
Copy after login

$num = 100000;echo $num < 10000 ? $num : sprintf("%0.1f",$num/10000) . '万';
Copy after login

$num = 100000;echo $num < 10000 ? $num : $num/10000 . '万';
Copy after login
Copy after login
Copy after login


简短精炼,值得信赖

$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 . '亿');}
Copy after login

根据4L得来的

$num = 100000;echo $num < 10000 ? $num : $num/10000 . '万';
Copy after login
Copy after login
Copy after login



独自面壁去。。。
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template