Number to Words..

  • Thread starter Thread starter VJ
  • Start date Start date
V

VJ

Is there any known component that would convert a value entered as 40,000 to
Forty Thousand..

Thanks
VJ
 
VJ,

Have you tried the static Parse method on the Int32 structure?
 
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.

Scott
 
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!)
 
... and no, I don't know of anything that'll do that. Sorry.

Below is an extremely crude piece of code which will do the job. Before you
all jump down my throat at how inefficient and inelegant it is, you're
absolutely right! It's not intended to be an exercise in concise coding,
rather an example that the OP can use to modify as required...

private string NumberToWords(string pstrNumber)
{
Hashtable htblNumbers = new Hashtable();
htblNumbers.Add(0, "zero");
htblNumbers.Add(1, "one");
htblNumbers.Add(2, "two");
htblNumbers.Add(3, "three");
htblNumbers.Add(4, "four");
htblNumbers.Add(5, "five");
htblNumbers.Add(6, "six");
htblNumbers.Add(7, "seven");
htblNumbers.Add(8, "eight");
htblNumbers.Add(9, "nine");
htblNumbers.Add(10, "ten");
htblNumbers.Add(11, "eleven");
htblNumbers.Add(12, "twelve");
htblNumbers.Add(13, "thirteen");
htblNumbers.Add(14, "fourteen");
htblNumbers.Add(15, "fifteen");
htblNumbers.Add(16, "sixteen");
htblNumbers.Add(17, "seventeen");
htblNumbers.Add(18, "eighteen");
htblNumbers.Add(19, "nineteen");
htblNumbers.Add(20, "twenty");
htblNumbers.Add(30, "thirty");
htblNumbers.Add(40, "forty");
htblNumbers.Add(50, "fifty");
htblNumbers.Add(60, "sixty");
htblNumbers.Add(70, "seventy");
htblNumbers.Add(80, "eighty");
htblNumbers.Add(90, "ninety");

string strOutput = "";
string[] astrNumber = pstrNumber.Split('.');

if(astrNumber[0].Length > 15)
{
return "Number is too long to convert";
}
else
{
astrNumber[0] = astrNumber[0].PadLeft(15, '0');
}

// trillions ------------------------------------------
if(Convert.ToInt32(astrNumber[0].Substring(0, 3)) > 0)
{
if(astrNumber[0].Substring(0, 1) != "1")
{
strOutput += htblNumbers[Convert.ToInt32(astrNumber[0].Substring(0,
1))] + " hundred";
}
if(Convert.ToInt32(astrNumber[0].Substring(1, 2)) > 0 &&
Convert.ToInt32(astrNumber[0].Substring(1, 2)) < 20)
{
strOutput += " and " +
htblNumbers[Convert.ToInt32(astrNumber[0].Substring(1, 2))];
}
if(Convert.ToInt32(astrNumber[0].Substring(1, 2)) > 19)
{
strOutput += " and " +
htblNumbers[Convert.ToInt32(astrNumber[0].Substring(1, 1) + "0")];
if(Convert.ToInt32(astrNumber[0].Substring(2, 1)) > 0)
{
strOutput += "-" +
htblNumbers[Convert.ToInt32(astrNumber[0].Substring(2, 1))];
}
}
strOutput += " trillion ";
}

// billions ------------------------------------------
if(Convert.ToInt32(astrNumber[0].Substring(3, 3)) > 0)
{
if(astrNumber[0].Substring(3, 1) != "1")
{
strOutput += htblNumbers[Convert.ToInt32(astrNumber[0].Substring(3,
1))] + " hundred";
}
if(Convert.ToInt32(astrNumber[0].Substring(4, 2)) > 0 &&
Convert.ToInt32(astrNumber[0].Substring(4, 2)) < 20)
{
strOutput += " and " +
htblNumbers[Convert.ToInt32(astrNumber[0].Substring(4, 2))];
}
if(Convert.ToInt32(astrNumber[0].Substring(4, 2)) > 19)
{
strOutput += " and " +
htblNumbers[Convert.ToInt32(astrNumber[0].Substring(4, 1) + "0")];
if(Convert.ToInt32(astrNumber[0].Substring(5, 1)) > 0)
{
strOutput += "-" +
htblNumbers[Convert.ToInt32(astrNumber[0].Substring(5, 1))];
}
}
strOutput += " billion ";
}

// millions ------------------------------------------
if(Convert.ToInt32(astrNumber[0].Substring(6, 3)) > 0)
{
if(astrNumber[0].Substring(6, 1) != "1")
{
strOutput += htblNumbers[Convert.ToInt32(astrNumber[0].Substring(6,
1))] + " hundred";
}
if(Convert.ToInt32(astrNumber[0].Substring(7, 2)) > 0 &&
Convert.ToInt32(astrNumber[0].Substring(7, 2)) < 20)
{
strOutput += " and " +
htblNumbers[Convert.ToInt32(astrNumber[0].Substring(7, 2))];
}
if(Convert.ToInt32(astrNumber[0].Substring(7, 2)) > 19)
{
strOutput += " and " +
htblNumbers[Convert.ToInt32(astrNumber[0].Substring(7, 1) + "0")];
if(Convert.ToInt32(astrNumber[0].Substring(8, 1)) > 0)
{
strOutput += "-" +
htblNumbers[Convert.ToInt32(astrNumber[0].Substring(8, 1))];
}
}
strOutput += " million ";
}

// thousands ------------------------------------------
if(Convert.ToInt32(astrNumber[0].Substring(9, 3)) > 0)
{
if(astrNumber[0].Substring(9, 1) != "1")
{
strOutput += htblNumbers[Convert.ToInt32(astrNumber[0].Substring(9,
1))] + " hundred";
}
if(Convert.ToInt32(astrNumber[0].Substring(10, 2)) > 0 &&
Convert.ToInt32(astrNumber[0].Substring(10, 2)) < 20)
{
strOutput += " and " +
htblNumbers[Convert.ToInt32(astrNumber[0].Substring(10, 2))];
}
if(Convert.ToInt32(astrNumber[0].Substring(10, 2)) > 19)
{
strOutput += " and " +
htblNumbers[Convert.ToInt32(astrNumber[0].Substring(10, 1) + "0")];
if(Convert.ToInt32(astrNumber[0].Substring(11, 1)) > 0)
{
strOutput += "-" +
htblNumbers[Convert.ToInt32(astrNumber[0].Substring(11, 1))];
}
}
strOutput += " thousand ";
}

// hundreds, tens and ones ---------------------------------------------
if(Convert.ToInt32(astrNumber[0].Substring(12, 3)) > 0)
{
if(astrNumber[0].Substring(12, 1) != "1")
{
strOutput += htblNumbers[Convert.ToInt32(astrNumber[0].Substring(12,
1))] + " hundred";
}
if(Convert.ToInt32(astrNumber[0].Substring(13, 2)) > 0 &&
Convert.ToInt32(astrNumber[0].Substring(13, 2)) < 20)
{
strOutput += " and " +
htblNumbers[Convert.ToInt32(astrNumber[0].Substring(13, 2))];
}
if(Convert.ToInt32(astrNumber[0].Substring(13, 2)) > 19)
{
strOutput += " and " +
htblNumbers[Convert.ToInt32(astrNumber[0].Substring(13, 1) + "0")];
if(Convert.ToInt32(astrNumber[0].Substring(14, 1)) > 0)
{
strOutput += "-" +
htblNumbers[Convert.ToInt32(astrNumber[0].Substring(14, 1))];
}
}
}

// decimal places ------------------------------------------------------
if(astrNumber.Length == 2)
{
strOutput += " point";
foreach (char strDigit in astrNumber[1].ToCharArray())
{
strOutput += " " + htblNumbers[Convert.ToInt32(strDigit.ToString())];
}
}

return strOutput.Replace(" ", " ");
}
 
Thanks Every one for the Input.... I will try and build what you have
given.. If I find some alogrithm I will share with group.!!


VJ
 
Back
Top