13,558,007 members
Alternative Tip/Trick
alternative version

#### Stats

10.2K views
5 bookmarked
Posted 19 Aug 2010
Licenced CPOL

# Converting numbers to the word equivalent.

, 19 Aug 2010
I'm sorry, but that's a horrible way to do it...Here is what I would do... public class NumberToWords { public static string ConvertToWords(int Number) { //Split the string into 3 part pieces string numStr = Number.ToString(); ...
I'm sorry, but that's a horrible way to do it...

Here is what I would do...
```    public class NumberToWords
{

public static string ConvertToWords(int Number)
{
//Split the string into 3 part pieces
string numStr = Number.ToString();
List<string> numParts = new List<string>();

string currentStr = "";

int t = 0;
for (int i = numStr.Length - 1; i >= 0; i--)
{
t++;
currentStr = numStr[i] + currentStr;

if (t == 3)
{
currentStr = "," + currentStr;
t = 0;
}
}

if (currentStr.StartsWith(","))
currentStr = currentStr.Remove(0, 1);

string[] vals = currentStr.Split(',');

List<string> outString = new List<string>();

List<string> delims = new List<string>();

if (numParts.Count >= 2)

if (numParts.Count >= 3)

if (numParts.Count >= 4)

if (numParts.Count >= 5)

int j = delims.Count - 1;

for (int i = 0; i < numParts.Count; i++)
{
int num = int.Parse(numParts[i]);

string temp = "";

if (num >= 100)
temp = ConvertThreeDigits(num);
else if (num >= 10)
temp = ConvertTwoDigits(num);
else
{
if (i == 0)
{
temp = ConvertOneDigit(num);
}
}

temp += " " + delims[j];
j--;

}

string retString = string.Join(" ", outString.ToArray());

return retString;
}

private static string ConvertThreeDigits(int Number)
{
int firstDigit = Number / 100;
int lastDigits = Number - (firstDigit * 100);

if (lastDigits == 0)
return ConvertOneDigit(firstDigit) + " Hundred";
else if (lastDigits < 9)
return ConvertOneDigit(firstDigit) + " Hundred " + ConvertOneDigit(lastDigits);
else
return ConvertOneDigit(firstDigit) + " Hundred " + ConvertTwoDigits(lastDigits);
}

private static string ConvertTwoDigits(int Number)
{
int firstDigit = Number / 10;
int secondDigit = Number - (firstDigit * 10);

if (Number >= 10 && Number < 20)
{
if (secondDigit == 4 || secondDigit == 6 ||
secondDigit >= 7)
{
return ConvertOneDigit(secondDigit) + "teen";
}
else
{
switch (secondDigit)
{
case 0:
return "Ten";
case 1:
return "Eleven";
case 2:
return "Twelve";
case 3:
return "Thirteen";
case 5:
return "Fifteen";
default:
return "ERROR";
}
}
}
else
{
string firstPart = "";

switch (firstDigit)
{
case 2:
firstPart = "Twenty";
break;
case 3:
firstPart = "Thirty";
break;
case 4:
firstPart = "Fourty";
break;
case 5:
firstPart = "Fifty";
break;
case 6:
firstPart = "Sixty";
break;
case 7:
firstPart = "Seventy";
break;
case 8:
firstPart = "Eighty";
break;
case 9:
firstPart = "Ninty";
break;
default:
return "ERROR";
}

if (secondDigit > 0)
return firstPart + "-" + ConvertOneDigit(secondDigit);
else
return firstPart;
}
}

private static string ConvertOneDigit(int Number)
{
switch (Number)
{
case 0:
return "Zero";
case 1:
return "One";
case 2:
return "Two";
case 3:
return "Three";
case 4:
return "Four";
case 5:
return "Five";
case 6:
return "Six";
case 7:
return "Seven";
case 8:
return "Eight";
case 9:
return "Nine";
default:
return "ERROR";
}
}
}
</string></string></string></string></string></string>```

## Share

 President 6D Systems LLC United States
I studied Software Engineering at Milwaukee School of Engineering for 2 years before switching to Management of Information Systems for a more business oriented approach. I've been developing software since the age of 14, and have waded through languages such as QBasic, TrueBasic, C, C++, Java, VB6, VB.NET, C#, etc. I've been developing professionally since 2002 in .NET.

## You may also be interested in...

 View All Threads First Prev Next
 If you are going to post code, at least check it works! Try ... OriginalGriff20-Aug-10 23:20 OriginalGriff 20-Aug-10 23:20
 Last Visit: 31-Dec-99 18:00     Last Update: 25-May-18 17:12 Refresh 1