9
  • I need to get the place value of a random number submitted by a user. This number can by anything from 0-1000000000000000 (zero to one trillion).

  • I think this can be achieved by using the JavaScript modulus % operator. The problem, I don't really know how to use it, nor do I understand it.

  • Here is the Fiddle.

(All I know is 10%3 returns 1 because 3*3 = 9 and 10-9 = 1)

I figured out the ones, tens, hundreds, and thousands:

var ones = Math.floor(num % 10),
    tens = Math.floor(num / 10 % 10),
    hundreds = Math.floor(num / 100 % 10),
    thousands = Math.floor(num % 10000 / 1000);

I just need:

  1. Ten thousands
  2. Hundred thousands
  3. Millions
  4. Ten millions
  5. Hundred millions
  6. Billions
  7. Ten billions
  8. Hundred billions
  9. Trillions

-- I don't even know if this is possible for the rest, but if you have any hints or find at least one, let me know! Thank you.

Matthew
  • 2,158
  • 7
  • 30
  • 52
  • "The problem, I don't really know how to use it, nor do I understand it." -- See here http://en.wikipedia.org/wiki/Modular_arithmetic – elclanrs Jun 15 '14 at 03:39
  • 2
    `num.toString(10).split("")`? What do you need the place values for? – Bergi Jun 15 '14 at 03:40
  • 1
    I'm making a number to word translator. – Matthew Jun 15 '14 at 03:41
  • @Matthew - Before I write out a long answer, can you just confirm that what you're trying to do is accept a numeric value such as 123456789 from the user and output "one hundred twenty-three million four hundred fifty-six thousand seven hundred eighty-nine"? – CptRobby Jun 16 '14 at 16:07
  • 1
    @CptRobby -- I appreciate your assistance. If you look at my answer below, you can see I figured it out! And yes that is what I am doing, but in Spanish. :) – Matthew Jun 16 '14 at 18:02
  • @Matthew - Ah. I don't really see how that's the answer exactly (especially since the pattern is inconsistent), but if it works for you, then I guess it's fine. I also don't really know how numbers are read in Spanish, so I won't try LOL. I would suggest you go ahead and accept an answer though so that the question doesn't show up as unanswered. ;) – CptRobby Jun 16 '14 at 18:13

4 Answers4

13
        var ones = Math.floor(num % 10),
            tens = Math.floor(num/10 % 10),
            hundreds = Math.floor(num/100 % 10),
            thousands = Math.floor(num/1000 % 10),
            tenThousands = Math.floor(num / 10000 % 10),
            hundredThousands = Math.floor(num / 100000 % 10),
            millions = Math.floor(num / 1000000 % 10),
            tenMillions = Math.floor(num / 10000000 % 10),
            hundredMillions = Math.floor(num / 100000000 % 10);

Managed to get to 100 million.

NishM
  • 1,706
  • 2
  • 15
  • 26
Matthew
  • 2,158
  • 7
  • 30
  • 52
0
var scaleStepWidth = 0;
var number = Math.ceil(1052220420.32);
var placeValue = 1;
for(i=0;i<(number.toString().length-1);i++){
    placeValue *= 10;
}
scaleStepWidth = eval(number.toString()[0])*placeValue;
Tunaki
  • 132,869
  • 46
  • 340
  • 423
Coder4web
  • 5
  • 2
0

The modulus function finds the remainder in division.

For example:

9 % 5 = 4 because the remainder from 9 / 5 is 4

Then we can construct a function to find any place value in a number. (This is all in regular Math because I'm not familiar with JavaScript but it's probably a smooth conversion)

GetDigit(x,y) = floor(mod(x/(10^floor(y)-1),10))

In Python it's:

import math
def GetDigit(x, y):
    return math.floor((x/((10^math.floor(y))-1)) % 10)

I know it's an old question but I couldn't see any other answers giving a definitive function.

off oof
  • 43
  • 5
0

Insted of declaring varaibles for all the places, this solution would give us an array of all the places

arr = [];
n = 39998989898989898 // your number here
m = 10;
while (n > 0) {
  i = n % m;
  arr.push(i);
  n -= i;
  m = m * 10;
}
console.log(arr);

This can work for more than one trillion