如何将数千个分隔符添加到数组中的数字,响应来自服务器

时间:2013-09-27 07:43:18

标签: javascript jquery json

如何检查数组并将千位分隔符添加到数千?

这是我的JSON:

{
"message": "Successfully  data has been fetched.",
"success": true,
"data": {
    "dataHeaders": [
        "1",
        "2",
        "3,
        "4",
        "5",
        "6"
    ],
    "dataArray": {
        "A": [
            "A",
            "45567",
            "545",
            "5",
            "10",
            "45",
            "24"
        ],
        "B": [
            "B",
            "40",
            "75465",
            "6",
            "6",
            "30",
            "25"
        ],
        "C": [
            "C",
            "30",
            "10",
            "3",
            "46565",
            "40",
            "65"
        ],
        "D": [
            "D",
            "307676",
            "10",
            "3",
            "4",
            "40",
            "65"
        ]
    }
}

}

在上面的JSON中我需要使用jQuery或JavaScript添加数组dataArray内的分隔符吗?

2 个答案:

答案 0 :(得分:0)

答案是,一如既往,正则表达式:

// add a comma every 3 digits, as a thousand separator
// only works with integer numbers
function number_format(n) {
    return String(n).replace(/\d(?=(?:\d\d\d)+$)/g, '$&,')
}

答案 1 :(得分:0)

我会在javascript中使用.toLocaleString:

类似的东西:

function number_format(n) {
  var int_language = "en-EN";
  return parseFloat(n).toLocaleString(int_language);
}