Re: Number to Words..
- From: "Mark Rae" <mark@xxxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Tue, 25 Oct 2005 10:21:20 +0100
"Scott Coonce" <sdcoonce@xxxxxxxxxxxxxxxxx> wrote in message
news:Ovl$zpT2FHA.3596@xxxxxxxxxxxxxxxxxxxxxxx
>I think he's going the other direction, Nicholas, ie:
>
> double number = double.Parse(textbox.Text);
> // number contains 40000
> string words = ConvertToWords(number);
> // words contains "Forty thousand"
>
> ... and no, I don't know of anything that'll do that. Sorry.
Indeed. This is a really simple, though incredibly long-winded procedure
involving an array thus:
0 zero
1 one
2 two
3 three
4 four
5 five
6 six
7 seven
8 eight
9 nine
10 ten
11 eleven
....
....
....
20 twenty
21 twenty-one
....
....
....
99 ninety-nine
1) You start off by stripping all the formatting e.g. 123,456,789.012 ->
123456789.012
2) Then you count the number of digits to the left of the decimal point, in
this case, 7 and consider them in groups of three padded with leading zeroes
e.g. 123 456 789
3) Next you take the first group (which you know to be "millions" because of
the number of digits to the left of the decimal point is 7) and convert it
to an integer e.g. 123.
If this value is less than 100, look it up in the array and append the
result to your output string.
If the value is 100 or more, do an integer division by 100 on it which, in
this case, returns 1. Look that up in the array i.e. "one" and append that
suffixed with " hundred" to your output string.
Do a Mod 100 on the value which, in this case, returns 23. Look that up in
the array i.e. "twenty-three" and append that prefixed with " and " and
suffixed with " million" to your output string.
Your output string now contains "one hundred and twenty-three million"
4) Do the same with with the thousands
5) Do the same with the hundreds, tens and ones.
6) Check if the original value contains a decimal point (that's a period to
our American friends!)
If so, add the word " point " to your output string, which now contains
"one hundred and twenty-three million four hundred and fifty-six thousand
seven hundred and eighty-nine point"
Finally, add the digits after the decimal point one by one.
Final result is:
"one hundred and twenty-three million four hundred and fifty-six thousand
seven hundred and eighty-nine point zero one two"
I'm sure that such an algorithm already exists on the net somewhere. No
doubt banks use something similar when printing cheques (that's checks to
our American friends!)
.
- References:
- Number to Words..
- From: VJ
- Re: Number to Words..
- From: Nicholas Paldino [.NET/C# MVP]
- Number to Words..
- Prev by Date: Re: How can I detect the presense of an optional assembly and call a method within it?
- Next by Date: Re: VS2005: Port project or recreate from ground up?
- Previous by thread: Re: Number to Words..
- Next by thread: Re: Number to Words..
- Index(es):