1

I am trying read integer and returning a string in C#?

For example : 1234 is one thousand, two hundred and thirty-four.

I was trying to do this for fun because I just learned C#. But this turn out to be difficult. Any guidance would help? So far I have found the length of the number given(eazy peazy) But the hard part is how to about it.

I'm not looking for the complete code, just constructive suggestions and a method/algorithm for going about this problem.

1 Answers1

0

I think the most helpful suggestion is to think of place value.

You know that 1xxx is "one thousand", 2xxx is "two thousand", etc. That's how the name of the number will start if the highest digit is in the thousands place.

The same is true for the hundreds place: 1xx is "one hundred", 2xx is "two hundred", etc. This would come immediately after the thousands place string, separated by a comma.

You'd need to prepare a string table to map simple digits (1–9) to text strings, and then you'd just concatenate those to either "hundred" or "thousand".

For the tens place, things get a bit trickier, because we don't say "three tens". English has special words for each of these, like "thirty" and "fifty". So you'd need another table mapping the digits 1–9 (when found in the tens place) to special names.

Finally, you have the ones place, which is, as you said, "eazy peazy". Use the same string table to map 1 to "one", 2 to "two", etc. Separate the tens-place string from the ones-place string with a hyphen.

So, for your example of 1234, you'd have:

  • "one" "thousand"
  • "two" "hundred"
  • "thirty"
  • "four"

…exactly the way you'd figure out how to say it in English! That code pretty much writes itself.

The only remaining challenge will be handling the case where one of these place values is missing, such as when it contains a 0. There might be a better way, but my instinct says you'll need conditional (if) statements to handle this.

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574