public class ConvertRupeesToWords
{
public static string ConvertNumbersToWords(int amountNumber)
{
int inputNo = amountNumber;
if (inputNo == 0)
return "Zero";
int[] numbers = new int[4];
int first = 0;
int u, h, t;
System.Text.StringBuilder sbWords = new System.Text.StringBuilder();
if (inputNo < 0)
{
sbWords.Append("Minus ");
inputNo = -inputNo;
}
string[] level0 = {"" ,"One ", "Two ", "Three ", "Four ",
"Five " ,"Six ", "Seven ", "Eight ", "Nine "};
string[] level1 = {"Ten ", "Eleven ", "Twelve ", "Thirteen ", "Fourteen ",
"Fifteen ","Sixteen ","Seventeen ","Eighteen ", "Nineteen "};
string[] level2 = {"Twenty ", "Thirty ", "Forty ", "Fifty ", "Sixty ",
"Seventy ","Eighty ", "Ninety "};
string[] level3 = { "Thousand ", "Lakh ", "Crore " };
numbers[0] = inputNo % 1000; // units
numbers[1] = inputNo / 1000;
numbers[2] = inputNo / 100000;
numbers[1] = numbers[1] - 100 * numbers[2]; // thousands
numbers[3] = inputNo / 10000000; // crores
numbers[2] = numbers[2] - 100 * numbers[3]; // lakhs
for (int i = 3; i > 0; i--)
{
if (numbers[i] != 0)
{
first = i;
break;
}
}
for (int i = first; i >= 0; i--)
{
if (numbers[i] == 0) continue;
u = numbers[i] % 10; // ones
t = numbers[i] / 10;
h = numbers[i] / 100; // hundreds
t = t - 10 * h; // tens
if (h > 0) sbWords.Append(level0[h] + "Hundred ");
if (u > 0 || t > 0)
{
if (t == 0)
sbWords.Append(level0[u]);
else if (t == 1)
sbWords.Append(level1[u]);
else
sbWords.Append(level2[t - 2] + level0[u]);
}
if (i != 0) sbWords.Append(level3[i - 1]);
}
return sbWords.ToString().TrimEnd();
}
}
{
public static string ConvertNumbersToWords(int amountNumber)
{
int inputNo = amountNumber;
if (inputNo == 0)
return "Zero";
int[] numbers = new int[4];
int first = 0;
int u, h, t;
System.Text.StringBuilder sbWords = new System.Text.StringBuilder();
if (inputNo < 0)
{
sbWords.Append("Minus ");
inputNo = -inputNo;
}
string[] level0 = {"" ,"One ", "Two ", "Three ", "Four ",
"Five " ,"Six ", "Seven ", "Eight ", "Nine "};
string[] level1 = {"Ten ", "Eleven ", "Twelve ", "Thirteen ", "Fourteen ",
"Fifteen ","Sixteen ","Seventeen ","Eighteen ", "Nineteen "};
string[] level2 = {"Twenty ", "Thirty ", "Forty ", "Fifty ", "Sixty ",
"Seventy ","Eighty ", "Ninety "};
string[] level3 = { "Thousand ", "Lakh ", "Crore " };
numbers[0] = inputNo % 1000; // units
numbers[1] = inputNo / 1000;
numbers[2] = inputNo / 100000;
numbers[1] = numbers[1] - 100 * numbers[2]; // thousands
numbers[3] = inputNo / 10000000; // crores
numbers[2] = numbers[2] - 100 * numbers[3]; // lakhs
for (int i = 3; i > 0; i--)
{
if (numbers[i] != 0)
{
first = i;
break;
}
}
for (int i = first; i >= 0; i--)
{
if (numbers[i] == 0) continue;
u = numbers[i] % 10; // ones
t = numbers[i] / 10;
h = numbers[i] / 100; // hundreds
t = t - 10 * h; // tens
if (h > 0) sbWords.Append(level0[h] + "Hundred ");
if (u > 0 || t > 0)
{
if (t == 0)
sbWords.Append(level0[u]);
else if (t == 1)
sbWords.Append(level1[u]);
else
sbWords.Append(level2[t - 2] + level0[u]);
}
if (i != 0) sbWords.Append(level3[i - 1]);
}
return sbWords.ToString().TrimEnd();
}
}