博客
关于我
javascript千位分隔
阅读量:389 次
发布时间:2019-03-05

本文共 1337 字,大约阅读时间需要 4 分钟。

??????

??????????????????????????????????????????????????????????????????

let num = 123456;  num.toLocaleString(); // ??: '123,456'

????????Node.js???????????????????????????????

????Node.js???????????

require('intl').NumberFormat().format(123456); // ??: '123,456'

?????????

????????????????????????????????????????????????????????????????

function numberWithCommas(x) {      var parts = x.toString().split(".");      parts[0] = parts[0].replace(/\B(?=(?:\d{3})+(?!\d))/g, ",");      return parts.join(".");  }

????????????????????????????????

console.log(numberWithCommas(23456324)); // ??: '23,456,324'

??????????????????????????????????????

????

????????????????????????????????????????????????????????????

function commaNum(num) {      let parts = num.toString().split('.');      parts[0] = ((str) => {          let tmp = '';          let counter = 0;          for(let i = str.length - 1; i >= 0; i--) {              if (counter == 3) {                  tmp += ',';                  i++;                  counter = 0;                  continue;              }              tmp += str[i];              counter++;          }          tmp = tmp.split('').reverse().join('');          return tmp;      })(parts[0]);      return parts.join('.');  }

???

console.log(commaNum(23456324)); // ??: '23,456,324'

???????????????????????????????

转载地址:http://nauwz.baihongyu.com/

你可能感兴趣的文章
Python 中常用的数据类型及相关操作详解
查看>>
Python 中的离线语音转文本
查看>>
Python 中的线程
查看>>
Python 中的继承机制是什么样的?
查看>>
Python 中的装饰器是什么?
查看>>
Python 中的装饰器是如何工作的,有哪些实际应用场景?
查看>>
python读excel
查看>>
Python 中读取 CSV 文件-ChatGPT4o作答
查看>>
python请求html_使用Python请求获取HTML?
查看>>
Python 之匿名函数和偏函数
查看>>
python 之栈的实现
查看>>
python语音播放
查看>>
python语言:装饰器原理
查看>>
Python 交互式数据可视化详解
查看>>
python语言有哪些优点和缺点_Python有哪些优缺点,你了解吗?
查看>>
Python 从入门到精通:30天速成教程到底有多狠?你能坚持下来吗?
查看>>
Python 从数据库中存储和检索密码的最安全方法
查看>>
Python语言及其应用 - 知识点遍历
查看>>
Python 优化提速的 8 个小技巧
查看>>
Python 余弦相似度与皮尔逊相关系数 计算
查看>>