0

如何检查数组并将千位分隔符添加到以千为单位的数字?

这是我的 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 中,我需要dataArray使用 jQuery 或 JavaScript 添加数组内部的分隔符吗?

4

2 回答 2

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, '$&,')
}
于 2013-09-27T07:50:48.773 回答
0

我会在 javascript 中使用 .toLocaleString:

就像是:

function number_format(n) {
  var int_language = "en-EN";
  return parseFloat(n).toLocaleString(int_language);
}
于 2013-09-27T08:19:16.260 回答