博客
关于我
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 日志管理封装
查看>>
python 时间函数小总结
查看>>
Python 是否优化尾递归?
查看>>
Python 更新set
查看>>
Python 最强 IDE 详细使用指南!
查看>>
Python 有一个不可变的列表吗?
查看>>
Python 机器学习入门之 pandas 的使用
查看>>
Python 机器学习实战
查看>>
python 杀死子进程_subprocess.popen.kill杀死所有子进程
查看>>
Python 条件语句与循环结构详解
查看>>
Python 条件运算符解决方法如何工作?
查看>>
python 枚举类型
查看>>
Python 查询 Mongodb数据库
查看>>
python 查询Neo4j多节点的多层关系
查看>>