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);
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);
Share
Improve this question
edited Nov 18, 2015 at 23:18
StateofDK
asked Nov 18, 2015 at 23:09
StateofDKStateofDK
1534 silver badges20 bronze badges
1
- Convert it in a string, then split it in substrings and that's all. – skypjack Commented Nov 18, 2015 at 23:11
4 Answers
Reset to default 15You 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.
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 ]
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;
}
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.