Don't listen to Fredbill
et. al. they're wasting your time.
The issue is how you are thinking about the number. Which is what LowestOne
is talking about.
Notice that the only difference between
123 ("one hundred twenty three") and 123,000 ("one hundred twenty three thousand") is the word "thousand" at the end.
This is true of any power of 103
What you have so far
You've got your basic formula for every group of three digits on lines 37 through 55. Put that into a function.
Now all you have to do is break your number up by groups of three (powers of 1000), and call the function for each group. Remember, you'll have to work backwards
, because you want to write the millions before the thousands before the zeros.
one hundred twenty three million
four hundred fifty six thousand
seven hundred eighty nine
 Working backwards really means that you have to grab the higher groups of three first.
int power_index = 1;
int power_value = 1000;
while ((number / power_value) != 0)
power_value *= 1000;
power_index += 1;
power_value /= 1000;
power_index -= 1;
// now power_value is one of 0, 1000, 1000000, 1000000000, ...
// and power_index is one of 0, 1, 2, 3, ...
Now you can use power_value
to divide number
for the first three digits to pass to your function. Use power_index
as an index into an array of the names to tack onto the end ("thousand", "million", etc).
Don't forget to remove those three digits from number
before the next time through the loop.
Hope this helps.