Introduction
A question which is often asked on programming forums is - how do I convert a number to words?
Although many solutions have been posted over the years, we appear to be lacking a basic article on the subject here on C# Corner.
In this article, I'd therefore like to present a simple program which I've written to deal with this question for integers in the range of an Int32 (about plus or minus 2 billion) which I believe is the most useful case.
The program supports both the US and UK numbering systems. For example, the number 620 would be expressed as follows:
Six Hundred Twenty (in the US system)
Six Hundred and Twenty (in the UK system)
Source Code
using System;
class Program
{
static void Main()
{
string input;
int number;
bool isValid;
bool isUK = false;
Console.WriteLine("\nEnter '0' to quit the program at any time\n");
while (true)
{
Console.Write("\nUse UK numbering y/n : ");
input = Console.ReadLine();
if (!(input.ToLower() == "y" || input.ToLower() == "n"))
Console.WriteLine("\n Must be 'y' or 'n', please try again\n");
else
{
if (input.ToLower() == "y") isUK = true;
Console.WriteLine("\n");
break;
}
}
do
{
Console.Write("Enter integer : ");
input = Console.ReadLine();
isValid = int.TryParse(input, out number);
if (!isValid)
Console.WriteLine("\n Not an integer, please try again\n");
else
Console.WriteLine("\n {0}\n", NumberToText(number, isUK));
}
while (!(isValid && number == 0));
Console.WriteLine("\nProgram ended");
}
public static string NumberToText(int number, bool isUK)
{
if (number == 0) return "Zero";
string and = isUK ? "and " : ""; // deals with UK or US numbering
if (number == -2147483648) return "Minus Two Billion One Hundred " + and +
"Forty Seven Million Four Hundred " + and + "Eighty Three Thousand " +
"Six Hundred " + and + "Forty Eight";
int[] num = new int[4];
int first = 0;
int u, h, t;
System.Text.StringBuilder sb = new System.Text.StringBuilder();
if (number < 0)
{
sb.Append("Minus ");
number = -number;
}
string[] words0 = {"", "One ", "Two ", "Three ", "Four ", "Five ", "Six ", "Seven ", "Eight ", "Nine "};
string[] words1 = {"Ten ", "Eleven ", "Twelve ", "Thirteen ", "Fourteen ", "Fifteen ", "Sixteen ", "Seventeen ", "Eighteen ", "Nineteen "};
string[] words2 = {"Twenty ", "Thirty ", "Forty ", "Fifty ", "Sixty ", "Seventy ", "Eighty ", "Ninety "};
string[] words3 = { "Thousand ", "Million ", "Billion " };
num[0] = number % 1000; // units
num[1] = number / 1000;
num[2] = number / 1000000;
num[1] = num[1] - 1000 * num[2]; // thousands
num[3] = number / 1000000000; // billions
num[2] = num[2] - 1000 * num[3]; // millions
for (int i = 3; i > 0; i--)
{
if (num[i] != 0)
{
first = i;
break;
}
}
for (int i = first; i >= 0; i--)
{
if (num[i] == 0) continue;
u = num[i] % 10; // ones
t = num[i] / 10;
h = num[i] / 100; // hundreds
t = t - 10 * h; // tens
if (h > 0) sb.Append(words0[h] + "Hundred ");
if (u > 0 || t > 0)
{
if (h > 0 || i < first) sb.Append(and);
if (t == 0)
sb.Append(words0[u]);
else if (t == 1)
sb.Append(words1[u]);
else
sb.Append(words2[t - 2] + words0[u]);
}
if (i != 0) sb.Append(words3[i - 1]);
}
return sb.ToString().TrimEnd();
}
}Conclusion
It would not be difficult to extend this program to deal with larger integers, with decimal numbers or with specific currencies and I leave this as an exercise to those who need this functionality.


Anandu G NathPosted Jan 6, 2024, 5:11 AM EST
Nice Article VUlpes
Kumaresh NatarajanPosted Aug 11, 2018, 6:39 AM EDT
Superb! Great!
Anil JhaPosted Jun 25, 2017, 1:09 AM EDT
Nice article.........
SharadPosted Jul 21, 2015, 8:25 PM EDT
good one...
Gowtham RajamanickamTOP 100Posted Mar 12, 2015, 9:55 PM EDT
Good article for learners....
Ken HPosted Dec 19, 2014, 4:40 AM EST
Thank you for sharing. :)
Satya RathorePosted Dec 18, 2014, 10:21 PM EST
Nice post you can get some what similar but little bit easy to understand code on below link <a href="http://www.getmscode.com/2014/12/convert-number-to-words-c-code.html"> Click here </a>
Vithal WadjeTOP 100Posted Nov 8, 2014, 7:05 AM EST
nice to see your article
VulpesTOP 100Posted Nov 8, 2014, 5:04 AM EST
Historically, a billion in the UK was a million millions i.e. 10^12. However, nowadays (officially from 1974) we use the same as the US i.e. a thousand million or 10^9.
jinesh samPosted Nov 8, 2014, 4:58 AM EST
Hi when i googled about the number system in UK & US i found that there is slight change in billon US:10^ 9 = 1,000,000,000 (Billion) UK 10 ^8 = 100,000,000 (UK Billion) How can i implement i this num[3] = isUK ? number / 1000000000 : number / 100000000; will this work?
Ken HPosted Aug 1, 2014, 8:12 PM EDT
I know,I think you are right.Digital units between bits and are separated by a 'and' in the United Kingdom?such as:the 105 you will say' One Hundred and Five'.the 650: say ' Six Hundred and Fifty'. the 228001: say 'Two Hundred and Twenty Eight Thousand and One'. the 6500000:say 'Six Millions and Five Hundred Thousand'. and so on...
VulpesTOP 100Posted Aug 1, 2014, 7:45 AM EDT
Well, I'd say 'One Hundred and Fifteen Thousand Six Hundred' personally because 'One Hundred and Fifteen Thousand and Six hundred' doesn't sound quite right to me. However, if the number were 115060, then I'd say 'One Hundred and Fifteen Thousand and Sixty' to emphasize the fact that there are no hundreds. I don't know whether there's an absolutely correct answer to something like this so I've just been guided by what sounds 'right' to me as a UK resident.
Ken HPosted Aug 1, 2014, 1:09 AM EDT
Nice Article!,But I have a question, in the uk system this 115600 said which one is correct? 'one hundred fifteen thousand and six hundred' and 'One Hundred and Fifteen Thousand Six Hundred' (The latter is the result of your program). Thank :)
VulpesTOP 100Posted Jul 30, 2014, 3:04 AM EDT
I've extended the NumberToText method so that it now deals with numbers in the range of a long integer (more than plus or minus 9 quintillion) in this forum thread: http://www.c-sharpcorner.com/Forums/Thread/263117/converting-one-hundred-trillion-digits-to-text.aspx
Nikhil KumarPosted Sep 6, 2013, 10:29 AM EDT
good work !!! keep going.
Palani KumareditedPosted Oct 1, 2012, 3:53 AM EDTEdited Oct 1, 2012, 3:55 AM EDT
good work friends......Expecting more from you.....
VulpesTOP 100Posted Apr 1, 2012, 12:42 PM EDT
If you check my blog entry (http://www.c-sharpcorner.com/Blogs/5951/converting-numbers-to-words-adding-decimal-support.aspx) there's a routine for adding decimal support to a similar program I did for the Indian numbering system which should also work for this US/UK numbering system program. So here you'd do: double d = double.Parse(label1.Text); int i = (int)d; string decimalPart = d.ToString().Split('.')[1]; label1.Text = NumberToText(i, false) + " Point" + DecimalToText(decimalPart);
VulpesTOP 100Posted Mar 31, 2012, 11:19 AM EDT
You first need to parse the label's text to an integer: int number = int.Parse(label1.Text). You can then call the NumberToText method and reassign the return value back to the label: label1.Text = NumberToText(number, false);
hani ahmadeditedPosted Mar 31, 2012, 5:31 AM EDTEdited Mar 31, 2012, 5:33 AM EDT
please can you give me the way to use this function in Label. with science that is allready number in that label?
Jitendra PateleditedPosted May 4, 2011, 4:31 AM EDTEdited May 4, 2011, 4:32 AM EDT
Hello dear, convert Number to Word convert in Indian Currency. like this: 125480 One Lakhs Twenty five thousand four hundred eighty. I have also send a post in www.C-sharpcorner.com site. Thanks, Jitendra Patel
Michael McGoniglePosted Mar 28, 2011, 1:44 AM EDT
I have noticed your articles over the past few weeks. They stand head and shoulders above the rest IMHO. They cover very interesting topics, introduce some great ideas and are by far the most professionally written articles submitted on a daily basis. Keep up the good work. I look forward to reading your next one!
Micke BlomqvistPosted Mar 24, 2011, 11:22 PM EDT
Will come in handy, thank's for sharing, Vulpes!