How to convert number to words

In many business application we require to convert the numbers to words, For example: Converting the number 1000 to One thousand. The Below C# program will help you in converting you numbers to words. The program is easy to understand and it can be modified easily.

num to word1

public string NumberToWords(int number)
        {
            if (number == 0)
                return "zero";
            if (number  0)
            {
                words += NumberToWords(number / 100000) + " Lakh ";
                number %= 100000;
            }
            if ((number / 1000) > 0)
            {
                words += NumberToWords(number / 1000) + " thousand ";
                number %= 1000;
            }
            if ((number / 100) > 0)
            {
                words += NumberToWords(number / 100) + " hundred ";
                number %= 100;
            }
            if (number > 0)
            {
                if (words != "")
                    words += "and ";
                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  0)
                        words += "-" + unitsMap[number % 10];
                }
            }
            return words;
        }
Rajat Jain
Rajat Jain

Latest posts by Rajat Jain (see all)

Leave a Reply