当前位置:网站首页>学习在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亿+
边栏推荐
猜你喜欢

路由/三层交换机DHCP下发地址详解【华为eNSP】

grafana手册之可视化配置图表table

蘑菇书EasyRL学习笔记

【正点原子STM32连载】第二章 STM32简介 摘自【正点原子】MiniPro STM32H750 开发指南_V1.1

TiDB升级与案例分享(TiDB v4.0.1 → v5.4.1)

命里有时终须有--记与TiDB的一次次擦肩而过

MindSpore:【AIR模型导出】导出时提示源码中select_op参数类型转换失败

Interview at 14:00 in the afternoon, I came out at 14:08 with my head down, asking too much...

思想茶叶蛋 (Jul 31,2022)| 元宇宙(Metaverse)下了一枚什么样的蛋

I am 37 this year, and I was rushed by a big factory to...
随机推荐
LeetCode简单题之最好的扑克手牌
路由/三层交换机DHCP下发地址详解【华为eNSP】
递归思想
[Punctuality Atom STM32 Serial] Chapter 2 STM32 Introduction Excerpted from [Punctual Atom] MiniPro STM32H750 Development Guide_V1.1
[Cloud Residency Co-Creation] HCSD Celebrity Live Streaming – Employment Guide
async - await
Detailed explanation of MSTP protocol configuration on Layer 3 switches [Huawei eNSP experiment]
《福格行为模型》:如何养成好习惯?
字符串与正则表达式(C#)
Quick tips for getting out of a single
我和 TiDB 的故事 | 缘份在,那就终是能相遇的
张朝阳对话俞敏洪:谈宇宙、谈焦虑、谈创业、谈退休、谈人生
MindSpore:Batchnorm only support nchw input!
layout manager
Interview at 14:00 in the afternoon, I came out at 14:08 with my head down, asking too much...
下午14:00面试,14:08低着头出来了 ,问的实在是太...
cannot import name ‘import_string‘ from ‘werkzeug‘【bug解决】
JSP基本语法
LVGL的多语言转换工具--字体设置的好助手
leetcode单调栈经典例题——最大矩形