So I've implemented the following code. Below function is applicable till 999999999.
private static string ConvertIntoWords(long number)
{
if (number == 0)
return "zero";
if (number < 0)
return "minus " + ConvertIntoWords(Math.Abs(number));
string words = "";
if ((number / 10000000) > 0)
{
words += ConvertIntoWords(number / 10000000) + " crores ";
number %= 10000000;
}
if ((number / 100000) > 0)
{
words += ConvertIntoWords(number / 100000) + " lacs ";
number %= 100000;
}
if ((number / 1000) > 0)
{
words += ConvertIntoWords(number / 1000) + " thousand ";
number %= 1000;
}
if ((number / 100) > 0)
{
words += ConvertIntoWords(number / 100) + " hundred ";
number %= 100;
}
if (number > 0)
{
var unitsMap = new[] { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen" };
var tensMap = new[] { "zero", "ten", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety" };
if (number < 20)
words += unitsMap[number];
else
{
words += tensMap[number / 10];
if ((number % 10) > 0)
words += " "+unitsMap[number % 10];
//words += "-" + unitsMap[number % 10];
}
}
return words;
}
The above function recursively calls itself and I've taken two arrays unitsMap and tensMap to find corresponding word to a digit.
No comments:
Post a Comment