当前位置:网站首页>学习在php中将特大数字转成带有千/万/亿为单位的字符串
学习在php中将特大数字转成带有千/万/亿为单位的字符串
2022-08-04 09:19:00 【徊忆羽菲】
学习在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亿+
边栏推荐
猜你喜欢

双指针方法

TiCDC同步延迟问题处理

leetcode经典例题——56.合并区间

sync-diff-inspector 使用实践

Fiddler(二)-手机抓包502错误解决方法

递归思想

VRRP + MSTP configuration, huawei eNSP experiment 】 【
![[Punctuality Atom STM32 Serial] Chapter 2 STM32 Introduction Excerpted from [Punctual Atom] MiniPro STM32H750 Development Guide_V1.1](/img/11/a97c9874a1c4b510e7ed9ec330a737.png)
[Punctuality Atom STM32 Serial] Chapter 2 STM32 Introduction Excerpted from [Punctual Atom] MiniPro STM32H750 Development Guide_V1.1

leetcode动态规划系列(求路径篇)
![一道[CSCCTF 2019 Qual]FlaskLight的详解再遇SSTI](/img/98/8b2359b7b99da9e50821cdaaf5e47d.png)
一道[CSCCTF 2019 Qual]FlaskLight的详解再遇SSTI
随机推荐
cannot import name 'import_string' from 'werkzeug' [bug solution]
Since his 97, I roll but he...
Four common methods of network attacks and their protection
思想茶叶蛋 (Jul 31,2022)| 元宇宙(Metaverse)下了一枚什么样的蛋
Inheritance and the static keyword
telnet远程登录aaa模式详解【华为eNSP】
The separation configuration Libpq is supported, speaking, reading and writing
关于技术学习的6个观点
今年37了,被大厂抢着要...
Could you please talk about how the website is accessed?[Interview questions in the web field]
获取cpu的核数
Anton Paar安东帕密度计比重计维修DMA35性能参数
Shell编程的条件语句
将jpg图片转换成yuv420(NV12)数据文件
tcp连接的细节
字符串与正则表达式(C#)
Apache Druid 实时分析数据库入门介绍
《福格行为模型》:如何养成好习惯?
MindSpore:mirrorpad算子速度过慢的问题
MindSpore:【AIR模型导出】导出时提示源码中select_op参数类型转换失败