首页> 实战笔录 >PHP开发笔记 >PHP PHP
在php中将特大数字转成带有千/万/亿为单位的字符串
作者:小萝卜 2022-11-02 【 PHP 】 浏览 1710
简介在php中将特大数字转成带有千/万/亿为单位的字符串
第一种方法:ifelse
/**
* @param int $num 要转换的阿拉伯数字
* @return string 转换成的字符串
*/
function convert($num)
{
if ($num >= 100000000) {
$num = round($num / 100000000, 1) . '亿+';
} else if ($num >= 10000000) {
$num = round($num / 10000000, 1) . '万+';
} else if ($num >= 10000) {
$num = round($num / 10000, 1) . '万+';
} else if ($num >= 1000) {
$num = round($num / 1000, 1) . '千+';
}
return $num;
}
第二种方法:switch
function convert_new($num)
{
switch ($num) {
case ($num > 100000000):
$num = round($num / 100000000, 1) . '亿+';
break;
case ($num > 10000000):
$num = round($num / 10000000, 1) . '千万+';
break;
case ($num > 10000):
$num = round($num / 10000, 1) . '万+';
break;
case ($num > 1000):
$num = round($num / 1000, 1) . '千+';
break;
}
return $num;
}
echo '1234=' . convert(1234);
echo "<br>";
echo '1234=' . convert_new(1234);
echo "<br>";
结果:
1234=1.2千+
1234=1.2千+
12345=1.2万+
12345=1.2万+
123456=12.3万+
123456=12.3万+
1234567=123.5万+
1234567=123.5万+
12345678=1.2万+
12345678=1.2千万+
123456789=1.2亿+
123456789=1.2亿+
1234567890=12.3亿+
1234567890=12.3亿+
很赞哦! (0)
相关文章
- bccomp在php中什么意思,PHP bccomp()用法及代码示例
- Windows系统下安装php ssh2扩展
- php自定义加密和解密,url参数加密,实现分享返利
- PHP常用数学函数分享
- php根据一个日期相对于当前时间,获得多久多久之前
- php获取网站运行了多长时间,服务器运行了多长时间,活动持续多长时间
- Namespace declaration statement has to be the very first statement or after any declare call in the
- wamp环境下PHP如何安装redis扩展
- PHP怎么判断访问的设备是ios还是安卓?
- 修改PHP上传文件大小限制的方法