当前位置:网站首页>数字加分隔符
数字加分隔符
2022-07-31 09:06:00 【大鲤余】
function _comma(number) {
// 补全代码
let num = String(number)
num = Array.from(num)//转成数组方便调用数组方法
num.reverse()//从后往前
for (let i = num.length - 1; i >= 0; i--) {
if (i % 3 == 0&&/\d/.test(num[i])) {
//每三位加,遇到负号不加,
num.splice(i, 0, ',')
}
}
if(num[0]==',')num.shift()//第一项如果,去掉
num.reverse()//改正确顺序
return num.join('')
}
//方法2递归
function _comma(number) {
// 补全代码
if (number < 1000) {
return number.toString();
} else {
//每三位,
return _comma(Math.floor(number / 1000)) + "," + _comma(number % 1000);//继续三位三位取
}
}
边栏推荐
猜你喜欢
随机推荐
UE4插件软链接(关联)
[Cloud native and 5G] Microservices support 5G core network
qt pass custom structure parameters in different threads
js实现2020年元旦倒计时公告牌
Flink1.15 source code reading - PER_JOB vs APPLICATION execution process
云服务器部署 Web 项目
Flutter Paystack implements all options
【Excel】生成随机数字/字符
2022 Hangzhou Electric Cup Super League 3
C# 正则表达式汇总
matlab常用符号用法总结
SSM framework explanation (the most detailed article in history)
canvas粒子变幻各种形状js特效
Browser usage ratio js radar chart
高并发高可用高性能的解决方案
刷题《剑指Offer》day06
Are postgresql range queries faster than index queries?
【节选】吴恩达给出的AI职业生涯规划
安装sambe
Small application project development, jingdong mall 】 【 uni - app custom search component (below) - search history