Numbers Into Words or Amount In Words In ASP.NET Core

In this article, we will look at how to convert numbers into words and display them as amounts (currency, rupees) in the C#.

Amount in Words: The most frequently asked question by developers and students is how to show the price or amount in an invoice by converting numbers to words.

Numbers to Words has numerous advantages for people who work with numbers on a daily basis. For example, if you need to programmatically convert large numbers to their English equivalent you can use the below example.

You can use this Number to Word converter when you are generating billing invoices where you are showing the amount as the word.

Step 1: First, let’s open Visual Studio and create a new project

Step 2: Add the Controller and Copy and paste the below code

[HttpPost]
       public JsonResult Convert(long numbers)
       {
           string amount = WordToNumber(numbers);
           amount = amount + " Rupees";  
           return Json(amount);
       }

       public string WordToNumber(long number)
       {

           string word = "";
           if (number == 0)
           {
               word += "Zero";
           }
           if ((number / 100000) > 0)
           {
               word += WordToNumber(number / 100000) + " Lakh ";
               number %= 100000;
           }
           if ((number / 1000) > 0)
           {
               word += WordToNumber((number / 1000)) + " thousand ";
               number %= 1000;
           }
           if ((number / 100) > 0)
           {
               word += WordToNumber((number / 100)) + " hundred ";
               number %= 100;
           }
           if (number > 0)
           {
               var unitmap = new[] {"One", "Two", "Three","Four","Five","Six","Seven","Eight","Nine","Ten","Eleven","Twelve","Thirteen","Fourteen","Fifteen","Sixteen","Seventeen","Eighteen",
                   "Nineteen" };
               var tensMap = new[] { "Ten", "Twenty", "Thirty", "Fourty", "Fifty", "Sixty", "Seventy", "Eight", "Ninety" };
               if (number < 20)
               {
                   word += " " + unitmap[number - 1];
               }
               else
               {
                   word += tensMap[(number / 10) - 1];
                   if ((number % 10) > 0)
                       word += " " + unitmap[(number % 10) - 1];

               }
           }
           return word;

       }

OutPut

Submit a Comment

Your email address will not be published. Required fields are marked *

Subscribe

Select Categories