当前位置:网站首页>学习在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同步延迟问题处理
IDEA 自动导入的配置(Auto import)
抬升市场投资情绪,若羽臣是否还需“自身硬”?
2022-08-02 分析RK817 输出32k clock PMIC_32KOUT_WIFI给WiFi模块 clock 注册devm_clk_hw_register
DOM简述
【正点原子STM32连载】第四章 STM32初体验 摘自【正点原子】MiniPro STM32H750 开发指南_V1.1
Post-94 Byte P7 posted the salary slip: It's really good to make up for this...
ISO14443A读卡流程(作为示例参考)
MindSpore:【mindinsight】【Profiler】用execution_time推导出来的训练耗时远小于真实的耗时
随机推荐
并发编程之生产者和消费者问题
三层交换机/路由器OSPF配置详解【华为eNSP实验】
layout manager
tcp连接的细节
MindSpore:【model_zoo】【resnet】尝试用THOR优化器运行时报cannot import name ‘THOR‘
今年37了,被大厂抢着要...
ISO14443A读卡流程(作为示例参考)
LVGL's multi-language conversion tool -- a good assistant for font settings
字符串与正则表达式(C#)
cannot import name ‘import_string‘ from ‘werkzeug‘【bug解决】
[Punctuality Atom STM32 Serial] Chapter 2 STM32 Introduction Excerpted from [Punctual Atom] MiniPro STM32H750 Development Guide_V1.1
Quick tips for getting out of a single
Apache APISIX 2.15 版本发布,为插件增加更多灵活性
户外徒步旅行
架构设计杂谈
Unity3D 数据加密
TCP的四次挥手
交换机链路聚合详解【华为eNSP】
MATLAB/Simulink快捷键
云函数实现网站自动化签到配置详解【Web函数/Nodejs/cookie】