博客
关于我
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/

你可能感兴趣的文章
NodeJs——(11)控制权转移next
查看>>
NodeJS、NPM安装配置步骤(windows版本)
查看>>
NodeJS、NPM安装配置步骤(windows版本)
查看>>
nodejs与javascript中的aes加密
查看>>
nodejs中Express 路由统一设置缓存的小技巧
查看>>
nodejs中express的使用
查看>>
Nodejs中的fs模块的使用
查看>>
NodeJS使用淘宝npm镜像站的各种姿势
查看>>
nodejs包管理工具对比:npm、Yarn、cnpm、npx
查看>>
NodeJs单元测试之 API性能测试
查看>>
nodejs图片转换字节保存
查看>>
nodejs在Liunx上的部署生产方式-PM2
查看>>
nodejs字符与字节之间的转换
查看>>
NodeJs学习笔记001--npm换源
查看>>
NodeJs学习笔记002--npm常用命令详解
查看>>
nodejs学习笔记一——nodejs安装
查看>>
NodeJS实现跨域的方法( 4种 )
查看>>
nodejs封装http请求
查看>>
nodejs常用组件
查看>>
nodejs开发公众号报错 40164,白名单配置找不到,竟然是这个原因
查看>>