how to split a number into groups of n digits

2020-07-23 04:37发布

I need to split a number into groups of digits and then place those digits into an array. I'll then be doing some simple math with those numbers and then inserting them into a textbox.

So far, I have only found how to split a number into individual digits as shown below:

var number = 12354987,
output = [],
sNumber = number.toString();

for (var i = 0, len = sNumber.length; i < len; i += 1) {
output.push(+sNumber.charAt(i));
}

console.log(output);

标签: javascript
4条回答
太酷不给撩
2楼-- · 2020-07-23 04:51

A fairly concise way of doing this would be to use a regular expression. For example, to split the number up into groups of up to 3 digits, you could use the following:

number.toString().match(/\d{1,3}/g)

This returns the following array:

[ "123", "549", "87" ]

To split into N digits, you can use the RegExp constructor instead:

var numDigits = 4;
var re = new RegExp("\\d{1," + numDigits + "}", "g");
var result = number.toString().match(re);

Then result would be:

[ "1235", "4987" ]

A simple way to change the values of result back into numbers would be to use map, with Number (which performs a type conversion in a non-constructor context):

var numbers = result.map(Number);

Then numbers would be:

[ 1235, 4987 ]
查看更多
聊天终结者
3楼-- · 2020-07-23 05:02

While there may be a simpler way to do this using Regexp, you could try this:

var splitNumber = function (number, digits) {
    var numString = number.toString();
    var numArray = [];

    for (var i = 0; i < numString.length; i+=digits) {
        numArray.push(numString.substring(i, i+digits));
    }

    for (var i = 0; i < numArray.length; i++) {
        numArray[i] = Number(numArray[i]);
    }
    return numArray;
}
查看更多
冷血范
4楼-- · 2020-07-23 05:02

Now that you have output with individual digits, you can use another array to split it into pieces of n size, like so:

var n = // your number n;
var final = [];
for(var i = 0; i < output.length / n; i++) {
    final[i] = [];
    for(var j = 0; j < n; j++) {
        final[i].push(output[(n*i)+j]);
    }
}

This outputs an array of arrays of size n.

If n = 2 it outputs the array (stored in final):

[
    [1, 2],
    [3, 5],
    [4, 9],
    [8, 7]
]

See working example on JSFiddle.net.

查看更多
The star\"
5楼-- · 2020-07-23 05:17

You can use regular expression to obtain the result you want.

If you want to group starting by left, the regular expression is quite simple:

// assuming you want to group every 3 digits
var number = 12354987,
    output = number.toString().match(/\d{1,3}/g);

// output: ["123", "549", "87"]

If, instead, you want to start by right side, e.g. like a currency format, the regular expression needs to be more articulate:

var number = 12354987,
    output = number.toString().match(/(\d+?)(?=(\d{3})+(?!\d)|$)/g);

// output: ["12", "354", "987"]

You can change the number of digit you want to group simply replacing 3 inside \d{3} with the amount desired. You could also obtain such value from a variable, in that case you have to use the RegExp constructor to build your regexp instance.

Edit: To convert the output to numbers again you can use the array's map method as follow:

var numbers = output.map(Number);

Hope it helps.

查看更多
登录 后发表回答