Edwin Edwin - 2 years ago 66
Javascript Question

How can I "break up" numbers into smaller pieces in javascript?

I think the title needs some explaining. I wan't to make my program break up a number into smaller bits.

For example, it would break 756 into 700, 50 and 6. 9123 would be 9000, 100, 20 and 3. Is there any way I can do this for any reasonably sized number?

Answer Source

Working Example

Here is a function that can do it:

function breakNumbers(num){
  var nums = num.toString().split('');
  var len = nums.length;
  var answer = nums.map(function(n, i) {
    return n + (Array(len - i - 1).fill(0)).join('');
    return answer.map(Number).filter(function(n) {return n !== 0;});
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download