当前位置:网站首页>数字加分隔符
数字加分隔符
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);//继续三位三位取
}
}
边栏推荐
- Small application project development, jingdong mall 】 【 uni - app custom search component (below) - search history
- 剑指offer-解决面试题的思路
- 安装gnome-screenshot截图工具
- postgresql 生成随机日期,随机时间
- skynet中一条消息从取出到处理完整流程(源码刨析)
- @RequestBody和@RequestParam区别
- 高并发-高可用-高性能
- Hematemesis summarizes thirteen experiences to help you create more suitable MySQL indexes
- ecshop安装的时候提示不支持JPEG格式
- 如何升级nodejs版本
猜你喜欢
[MySQL exercises] Chapter 4 · Explore operators in MySQL with kiko
科目三:右转弯
【NLP】Transformer理论解读
Browser usage ratio js radar chart
The future of the hybrid interface: conversational UI
【TCP/IP】Network Model
【TCP/IP】网络模型
js department budget and expenditure radar chart
[转载] Virtual Studio 让系统找到需要的头文件和库
来n遍剑指--06. 从尾到头打印链表
随机推荐
Canvas particles change various shapes js special effects
JSP response,request操作中(中文乱码)-如何解决呢?
Scala基础【seq、set、map、元组、WordCount、队列、并行】
利用frp服务器进行内网穿透ssh访问
刷题《剑指Offer》day05
【Unity】编辑器扩展-02-拓展Hierarchy视图
Golang-based swagger super intimate and super detailed usage guide [there are many pits]
Andoird开发--指南针(基于手机传感器)
spark filter
【MySQL中auto_increment有什么作用?】
[转载] Virtual Studio 让系统找到需要的头文件和库
科目三:右转弯
[Mini Program Project Development--Jingdong Mall] Custom Search Component of uni-app (Middle)--Search Suggestions
[MySQL exercises] Chapter 4 · Explore operators in MySQL with kiko
The future of the hybrid interface: conversational UI
如何在一台机器上(windows)安装两个MYSQL数据库
2019 NeurIPS | Graph Convolutional Policy Network for Goal-Directed Molecular Graph Generation
02 Truffle TutorialToken 示例
qt在不同的线程中传递自定义结构体参数
7. JS ES6新增语法 new Map详讲,还有一道代码实战案例帮你快上手new Map