Marvin kakuru

Marvin kakuru

  • 1.4k
  • 356
  • 160.2k

converting one hundred trillion (digits) to text

Jul 30 2014 4:56 AM
hi everyone.
 
I need help converting numbers to text. the code i am using only converts up to TWO BILLION.But i need to convert up to One hundred Trillion.
 
below is the code i have been using.
 
public static string NumberToText(int number, bool isUK)
{
if (number == 0) return "Zero";
string and = isUK ? "and " : ""; // deals with UK or US numbering
if (number == -2147483648) return "Minus Two Billion One Hundred " + and +
"Forty Seven Million Four Hundred " + and + "Eighty Three Thousand " +
"Six Hundred " + and + "Forty Eight";
int[] num = new int[4];
int first = 0;
int u, h, t;
System.Text.StringBuilder sb = new System.Text.StringBuilder();
if (number < 0)
{
sb.Append("Minus ");
number = -number;
}
string[] words0 = { "", "One ", "Two ", "Three ", "Four ", "Five ", "Six ", "Seven ", "Eight ", "Nine " };
string[] words1 = { "Ten ", "Eleven ", "Twelve ", "Thirteen ", "Fourteen ", "Fifteen ", "Sixteen ", "Seventeen ", "Eighteen ", "Nineteen " };
string[] words2 = { "Twenty ", "Thirty ", "Forty ", "Fifty ", "Sixty ", "Seventy ", "Eighty ", "Ninety " };
string[] words3 = { "Thousand ", "Million ", "Billion " };
num[0] = number % 1000; // units
num[1] = number / 1000;
num[2] = number / 1000000;
num[1] = num[1] - 1000 * num[2]; // thousands
num[3] = number / 1000000000; // billions
num[2] = num[2] - 1000 * num[3]; // millions
for (int i = 3; i > 0; i--)
{
if (num[i] != 0)
{
first = i;
break;
}
}
for (int i = first; i >= 0; i--)
{
if (num[i] == 0) continue;
u = num[i] % 10; // ones
t = num[i] / 10;
h = num[i] / 100; // hundreds
t = t - 10 * h; // tens
if (h > 0) sb.Append(words0[h] + "Hundred ");
if (u > 0 || t > 0)
{
if (h > 0 || i < first) sb.Append(and);
if (t == 0)
sb.Append(words0[u]);
else if (t == 1)
sb.Append(words1[u]);
else
sb.Append(words2[t - 2] + words0[u]);
}
if (i != 0) sb.Append(words3[i - 1]);
}
return sb.ToString().TrimEnd();
}
private void textBox8_TextChanged(object sender, EventArgs e)
{
int number;
string text = textBox8.Text.Trim().Replace(",", "");
bool isValid = int.TryParse(text, out number);
if (!isValid)
{
MessageBox.Show("Not a valid number. Please amend");
textBox8.Focus();
return;// assuming you're in some eventhandler
}

label5.Text = (NumberToText(number, false))+ " " + label14.Text +"S"+" "+ "Only";
}
}
}

any help will be highly appreciated
 

Answers (3)