*Edited* EVEN MORE *Edited* Boosted Silic0re09 version. It will work with up to 10^3002 or 3003 characters or novenonagintanongentillion. I've also refactored it a bit for you to have a better grade. For sanity sake, I've put the delimiters in text files. You can download them here:
http://www.filedropper.com/us[
^]
public class NumberToWords
{
List<string> delims;
List<string> teen;
List<string> secondDigit;
List<string> singleDigit;
string hundred;
public NumberToWords()
{
delims = TextLineIntoList("US/delims.txt");
teen = TextLineIntoList("US/teen.txt");
secondDigit = TextLineIntoList("US/secondDigit.txt");
singleDigit = TextLineIntoList("US/singleDigit.txt");
hundred = TextLineIntoList("US/hundred.txt")[0];
}
public int maxLength()
{
return (delims.Count + 1) * 3;
}
public string ConvertToWords(string Number)
{
if(!string.IsNullOrEmpty(Number))
{
string numStr = Number;
List<string> numParts = SplitNumberIntoParts(numStr);
bool zero = true;
for(int i = 0; i < numParts.Count && zero; i++)
{
zero = int.Parse(numParts[i]) == 0;
}
List<string> outString = new List<string>();
if(zero)
{
outString.Add(singleDigit[0]);
}
else
{
int j = numParts.Count - 2;
for(int i = 0; i < numParts.Count; i++)
{
int num = int.Parse(numParts[i]);
string temp = "";
if(num != 0)
{
if(num >= 100)
{
temp = ConvertThreeDigits(num);
}
else if(num >= 10)
{
temp = ConvertTwoDigits(num);
}
else
{
temp = ConvertOneDigit(num);
}
if(j >= 0)
temp += " " + delims[j] + ", ";
}
j--;
outString.Add(temp);
}
}
string retString = string.Join("", outString.ToArray());
return retString;
}
else
{
return "";
}
}
private static List<string> TextLineIntoList(string path)
{
TextReader tr = new StreamReader(path);
string line;
List<string> delims = new List<string>();
while(!string.IsNullOrEmpty(line = tr.ReadLine()))
{
delims.Add(line);
}
tr.Close();
return delims;
}
private List<string> SplitNumberIntoParts(string numStr)
{
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> result = new List<string>(vals);
return result;
}
private 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 string ConvertTwoDigits(int Number)
{
int firstDigit = Number / 10;
int secondDigit = Number - (firstDigit * 10);
if(Number >= 10 && Number < 20)
{
return teen[secondDigit];
}
else
{
string firstPart = this.secondDigit[firstDigit -2];
if(secondDigit > 0)
firstPart += "-" + ConvertOneDigit(secondDigit);
return firstPart;
}
}
private string ConvertOneDigit(int Number)
{
return singleDigit[Number];
}
}
I have a three years technical education in computer science (June 2006) and a four years bachelor's degree in Software Engineering from the École de Technologie Supérieure (August 2010). I worked as a Web Developper at the beginning of my career before being recruited as a .NET Analyst-Programer at SNC-Lavalin in Montreal. I worked there until they outsourced all development to CGI. I worked there since.