Hello Everyone,
I'm looking for a way to convert a Double like 1.200 to a text "One Thousand Two Hundred".
Is it possible? I know its possible in excell but how to do in C#
Hello Everyone,
I'm looking for a way to convert a Double like 1.200 to a text "One Thousand Two Hundred".
Is it possible? I know its possible in excell but how to do in C#
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Vishal YelveRank #178 · 13.6K reputationPosted Sep 13, 2023, 5:47 AM EDT
Hi Martin,
https://www.aspdotnet-suresh.com/2014/08/aspnet-convert-numbers-to-words-string-in-csharp.html
http://www.softwareandfinance.com/CSharp/Number_To_Text.html
https://interviewsansar.com/program-to-convert-number-to-words-in-csharp/
Amit MohantyRank #112 · 20K reputationPosted Sep 13, 2023, 6:09 AM EDT
This is not the perfact solutions, but it will work for you:
namespace MyConsoleApp { class Program { static void Main(string[] args) { double number = 1.200; string numberAsString = number.ToString("F3"); // Convert the double to a string with 3 decimal places string numberWithoutDot = numberAsString.Replace(".", ""); // Remove the dot int number1 = int.Parse(numberWithoutDot); string result = NumbersToWords.ConvertNumber(number1); Console.WriteLine(result); } } class NumbersToWords { private static readonly string[] units = { "Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen" }; private static readonly string[] tens = { "", "", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety" }; public static string ConvertNumber(double dblNumber) { try { long intNumber = (long)dblNumber; long decNumber = (long)Math.Round((dblNumber - intNumber) * 100); if (decNumber == 0) { return Convert(intNumber); } else { return Convert(intNumber) + " Point " + Convert(decNumber); } } catch (Exception e) { // TODO: handle exception Debug.WriteLine(e.Message); } return ""; } internal static string Convert(long i) { if (i 0) ? " " + Convert(i % 10) : ""); } if (i 0) ? " And " + Convert(i % 100) : ""); } if (i 0) ? " " + Convert(i % 1000) : ""); } if (i 0) ? " " + Convert(i % 100000) : ""); } if (i 0) ? " " + Convert(i % 10000000) : ""); } return Convert(i / 1000000000) + " Arab " + ((i % 1000000000 > 0) ? " " + Convert(i % 1000000000) : ""); } } }Rajeesh MenothRank #118 · 18.4K reputationPosted Sep 13, 2023, 5:24 AM EDT
Hi,
An article on the same topic was authored by a member of the C# Corner community - https://www.c-sharpcorner.com/blogs/convert-number-to-words-in-c-sharp
Similar thread in Stack Overflow - https://stackoverflow.com/questions/2729752/converting-numbers-in-to-words-c-sharp
Mohammad HussainRank #513 · 4.8K reputationPosted Sep 14, 2023, 3:03 AM EDT
using System; using System.Text; public class Program { static void Main() { double number = 1200.00; string words = ConvertDoubleToWords(number); Console.WriteLine(words); // Output: "One Thousand Two Hundred" } public static string ConvertDoubleToWords(double number) { // Split the double into integer and fractional parts long integerPart = (long)number; double fractionalPart = number - integerPart; // Convert the integer part to words string integerWords = ConvertToWords(integerPart); // Convert the fractional part to words string fractionalWords = ConvertFractionToWords(fractionalPart); // Combine the integer and fractional parts StringBuilder result = new StringBuilder(); result.Append(integerWords); if (!string.IsNullOrEmpty(fractionalWords)) { result.Append(" and "); result.Append(fractionalWords); } return result.ToString(); } public static string ConvertToWords(long number) { // Define the word equivalents for digits string[] units = { "Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine" }; string[] teens = { "", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen" }; string[] tens = { "", "Ten", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety" }; string[] thousands = { "", "Thousand", "Million", "Billion", "Trillion" }; int groupIndex = 0; string words = ""; while (number > 0) { long group = number % 1000; if (group > 0) { string groupWords = ""; if (group >= 100) { groupWords += units[group / 100] + " Hundred"; group %= 100; if (group > 0) groupWords += " and "; } if (group >= 20) { groupWords += tens[group / 10]; group %= 10; if (group > 0) groupWords += " "; } if (group > 10 && group 0) { groupWords += units[group]; } words = groupWords + " " + thousands[groupIndex] + " " + words; } number /= 1000; groupIndex++; } return words.Trim(); } public static string ConvertFractionToWords(double fractionalPart) { // Limit the precision of the fractional part to two decimal places fractionalPart = Math.Round(fractionalPart, 2); if (fractionalPart == 0) { return ""; // No fractional part } // Extract the fractional part as two digits int fractionalDigits = (int)(fractionalPart * 100); // Convert the two digits to words string fractionalWords = ConvertToWords(fractionalDigits); // Add "Cents" or "Cent" at the end return fractionalWords + (fractionalDigits == 1 ? " Cent" : " Cents"); } }Jayraj ChhayaRank #304 · 8.2K reputationPosted Dec 22, 2023, 1:09 AM EST
In C#, there is no built-in method to directly convert a
Doublevalue to its textual representation in words. However, you can achieve this by breaking down the number into its individual parts (thousands, hundreds, tens, and units) and then converting each part to its corresponding word representation.Here's an example of how you can accomplish this using C#:
public static string ConvertDoubleToText(double number) { string[] units = { "", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine" }; string[] teens = { "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen" }; string[] tens = { "", "", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety" }; if (number == 0) { return "Zero"; } string words = ""; if (number 0) { words += ConvertToWords(intPart) + " "; } if (decimalPart > 0) { words += "and " + ConvertToWords(decimalPart); } return words.Trim(); } private static string ConvertToWords(int number) { if (number < 10) { return units[number]; } else if (number < 20) { return teens[number - 10]; } else if (number < 100) { return tens[number / 10] + " " + units[number % 10]; } else if (number < 1000) { return units[number / 100] + " Hundred " + ConvertToWords(number % 100); } else if (number < 1000000) { return ConvertToWords(number / 1000) + " Thousand " + ConvertToWords(number % 1000); } else if (number < 1000000000) { return ConvertToWords(number / 1000000) + " Million " + ConvertToWords(number % 1000000); } else { return ConvertToWords(number / 1000000000) + " Billion " + ConvertToWords(number % 1000000000); } }You can use the
ConvertDoubleToTextmethod to convert aDoublevalue to its textual representation. For example:double number = 1200; string text = ConvertDoubleToText(number); Console.WriteLine(text); // Output: "One Thousand Two Hundred"Please note that this implementation is limited to converting numbers up to billions. You can modify it to support larger numbers if needed.
I hope this helps! Let me know if you have any further questions.
Anandu G NathRank #681 · 3.5K reputationPosted Dec 21, 2023, 1:14 AM EST
Use the below Function
public static class NumberToWordsConverter { private static readonly string[] Ones = { "Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine" }; private static readonly string[] Teens = { "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen" }; private static readonly string[] Tens = { "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety" }; private static readonly string[] Thousands = { "", "Thousand", "Million", "Billion", "Trillion" }; public static string ConvertToWords(double number) { if (number == 0) return Ones[0]; string words = ""; int i = 0; while (number > 0) { if (number % 1000 != 0) { words = $"{ConvertChunk((int)(number % 1000))} {Thousands[i]} {words}"; } number /= 1000; i++; } return words.Trim(); } private static string ConvertChunk(int number) { string chunk = ""; if (number >= 100) { chunk += $"{Ones[number / 100]} Hundred "; number %= 100; } if (number >= 10 && number = 20) { chunk += $"{Tens[number / 10 - 2]} "; number %= 10; } if (number >= 1 && number <= 9) { chunk += Ones[number]; } return chunk; } }Public void Convert() { double Number = 1234567.89; // Replace this with your double value string result = NumberToWordsConverter.ConvertToWords(Number ); }