JSON网格中有千个分隔的数字

时间:2017-11-03 05:03:50

标签: javascript json grid numbers format

在JSON网格中,一列定义为: -

{
"dataField": "Number of Days",
"caption":  "Number of Days",
"dataType": "number",
}

输出是:

Number of days 
4125490

我期待的是:数字应以逗号显示千位分隔值

4125497

请帮忙!

3 个答案:

答案 0 :(得分:1)

var n = 34523453.345
n.toLocaleString() //returns "34,523,453.345"

答案 1 :(得分:0)

您可以使用正则表达式

为千位分隔符创建一个函数



function thousandSeparator(num) {
  return num.toString().replace(/(\d)(?=(\d{3})+(?!\d))/g, "$1,")
}

console.log(thousandSeparator(4125490))




或者使用toLocaleString添加逗号。这两种方式都会以字符串格式返回结果



console.log((4125490).toLocaleString('en'))




答案 2 :(得分:0)

let numStr = "41123123.1231"
let x = numStr.split('.')[0]
let commaString = ""
for (i = 0; i<x.length ; i++){ 
  commaString+= x[i]; 
  if ((x.length-i-1) % 3 ===0 && (x.length-i-1)!==0) {
    commaString += ',';
  }
}
commaString = numStr.split('.')[1] ? [commaString, numStr.split('.')[1]].join('.') : commaString
console.log(commaString)