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.

Community
  • 1
  • 1
  • 1
    For doing this in all the languages you can think of: http://stackoverflow.com/questions/309884/code-golf-number-to-words – Aamir Sep 21 '10 at 06:12

2 Answers2

1

Try this:
http://letsblogabout.net/post/Converting-Numbers-to-Words.aspx

Also see: this question

Community
  • 1
  • 1
NullUserException
  • 83,810
  • 28
  • 209
  • 234
0

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
  • 4,437
  • 2
  • 29
  • 28