How can I convert numbers to their word form?

closed account (28UMizwU)
???
Last edited on
I'm not clear on the calculations you do at the top. I'd expect something like:
1
2
3
4
5
6
7
thousands = usrnum / 1000;
usrnum -= 1000*thousands;

hundreds = usrenum / 100;
usrnum -= 100*hundreds;

// and so on 
if you have third is one then you should not run the swith for last and third and move the ten from the third swith to thirs swich
You're calculating both thirs and third and there is no conditional so both thirs and third's switch statement will return a value.

How about you set thirs value ONLY if the number is eligible to be a teen. And initialize thirs to something not on the switch. ;)

By the way in the future if you're trying to implement 5 digit numbers for example, this logic won't hold. So better you get the digits in another way (you should not depend on counting the number from left to right of the number to get the digit you have to go right to left always).

How about you use your number % 10 itself? And then divide integer-number / integer-10 to get quotient of the number without the last digit so you can number % 10 this one too?
Hello maxtm92,

You are not the first to do this type of program. Try a search on "numbers to words" or "numbers to english". There are many links that can shorten the switch/case statements.

Hope that helps,

Andy
I think you need to look at the english again.
one, two, ... is a reused base
the teens are exceptions, so 11 to 19 inclusive are unique results, may as well make it 10-19.
then 20,30,... reuse 1-9 (so you need words twenty, thirty, etc)
then hundred becomes a word, but it reuses the rest: one (1-9 reused) hundred fifty (reused) six (1-9 reused) for example.
then thousand becomes a word, and everything else is reused.
and so on up the pow(10,...) chain of words... million/billion/etc

writing one thousand as one string is not the best way, or one hundred as one string... you should be remixing the existing texts as much as possible.
Last edited on
Topic archived. No new replies allowed.