Is there a mathematical way to get the following:

For the the number 777,888,999 so 777 million and etc etc

getMillions(777888999) should return 777 only

getThousands(777888999) should return 888 only

getHundreds(777888999) should return 999 only

This should be done without using regex and without text processing but should be achieved using only mathematical operations.

I'm trying to solve a problem that may be familiar to some which is converting a number expressed in digits to words so 1 -> one, 10 -> ten, 100 -> one hundred etc

My thinking is that if I can convert a number up to 999 then the problem is pretty much solved as the rest will only append the words millions and/or thousands so

777888999 will covert to the words:

777 million and 888 thousands and 999 hundred

of course the above numbers will expressed in words.

This originally was given to me as part of a job interview process about a year ago. I am absolutely not using the solution here for a job application. But the above question is about number manipulation which I'm very interested in.

  • 1
    Sharing your research helps everyone. Tell us what you've tried and why it didn’t meet your needs. This demonstrates that you’ve taken the time to try to help yourself, it saves us from reiterating obvious answers, and most of all it helps you get a more specific and relevant answer. Also see How to Ask. – ratchet freak Mar 7 '14 at 21:01
  • 2
    What do you get when you divide 777,888,999 by one million and throw away the remainder? – Robert Harvey Mar 7 '14 at 21:03
  • 6
    This question appears to be off-topic because it is about math. – Robert Harvey Mar 7 '14 at 21:04
  • 1
    Everything a computer does is math: it computes. – user22815 Mar 7 '14 at 21:26

You could do something like this:

print (777888999/1000000)%1000
print (777888999/1000)%1000
print (777888999)%1000

So, divide by the 1th unit of the position you are looking for, and then just mod it by 1000 since all of these positions can have three places.

  • Nice! indeed, very nice. – jakstack Mar 7 '14 at 21:23
  • If you don't know how long the number is, you can use log() to figure out how many digits are in it. – Sean McSomething Mar 8 '14 at 1:01
  • Sounds interesting, can you give an example using Java? – jakstack Mar 8 '14 at 7:54

Not the answer you're looking for? Browse other questions tagged or ask your own question.