Click here to Skip to main content
15,867,488 members
Please Sign up or sign in to vote.
1.20/5 (2 votes)
I want to convert a number which will come from my database into words.

Like I get a value "52391.00" from my database. Now i want to convert it as "Fifty-Two Thousand Three Hundred Ninety One"...


Please Help!!!!!
Posted
Comments
LLLLGGGG 18-Jun-15 3:07am    
Look at this link: https://www.exchangecore.com/blog/convert-number-words-c-sharp-console-application/

That's a secret, you cannot Google for it[^].
 
Share this answer
 
You Can Use This Method
C#
public static string NumberToWords(int number)
       {
           if (number == 0) { return "zero"; }
           if (number < 0) { return "minus " + NumberToWords(Math.Abs(number)); }
           string words = "";
           if ((number / 10000000) > 0) { words += NumberToWords(number / 10000000) + " Crore "; number %= 10000000; }
           if ((number / 100000) > 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 < 20) { words += unitsMap[number]; }
               else { words += tensMap[number / 10]; if ((number % 10) > 0) { words += "-" + unitsMap[number % 10]; } }
           }
           return words;
       }
 
Share this answer
 
Comments
Member 14712602 1-Feb-20 6:28am    
how to call this code
Check out this project on GitHub - Humanizer
https://github.com/MehdiK/Humanizer[^]
 
Share this answer
 
Check another codeproject question with an answer

how to convert number into words in C#[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900