tags:

views:

97

answers:

2

Possible Duplicate:
How can I convert an integer into its verbal representation?

Consider i have a 5 digit number say 45456 and i want to print it as Forty five thousand four hundred and fifty six using c#. Any suggestion.

A: 

I dont feel like writing out the code (plus its your homework, so you can do that part), but here's what you'll need to do:

  • you'll need to look at your numbers in groups of three so that you can figure out strings like "forty five thousand" and "four hundred and fifty six". especially since each group only affects the numbers in that group.
  • the position of each group affects which modifiers apply to them.
  • I would create a list of which modifiers apply to which group, and as you look at each group, you can dynamically be applying modifiers. that way your loop wont need to care about where it is in the number.

NB. my points are really, really generic. so I'm looking at numbers that can be more than just 5 digits. if it's JUST five digits you care about, you can solve it with just a basic switch statement that substitutes numbers from a list:

given 45456:

  • subsitute the first number with with the sub-100 value (so 2 is replaced with 'twenty', 3 with 'thirty', 4 with 'forty', etc)

  • subsitute the second number with the straight up value (so 2 is 'two', 3 is 'three', etc)

  • add the word 'thousand'

  • subsitute the third number with a hundreds value (so 2 is 'two hundred', 3 is 'three hundred')

  • subsitute the fourth number with the sub-100 value (so 2 is replaced with twenty, etc)

  • subsitute the fifth number with the straight up value.

note that the first and second steps are identical to the last two.

I hope this helps :)

Oren Mazor
Doesn't account for numbers like 15 or 12003.
cHao
True but that's another case to handle as an alternative to the first two steps.
Oren Mazor