Click here to Skip to main content
12,297,981 members (49,505 online)
Click here to Skip to main content
Add your own
alternative version

Stats

115.7K views
4.9K downloads
31 bookmarked
Posted

How to Convert a Numeric Value or Currency to English Words Using C#

, 11 Oct 2006 CPOL
Rate this:
Please Sign up or sign in to vote.
This article includes a sample application that will show you how to convert a numeric value to an English word representation.

Introduction

After looking around the web for this functionality and failing to get an answer to my problem. I decided to spare some minutes and develop this functionality up to my requirement.

I've decided to share with developers out who may require it.  

The program accepts numbers/numeric values as input and outputs an an English word representation.It accepts numbers ranging from 1 to some billions.

Here is the code:

using System;
namespace custom.util
{
public class NumberToEnglish
{
public String changeNumericToWords(double numb)
{
String num = numb.ToString();
return changeToWords(num, false);
}
public String changeCurrencyToWords(String numb)
{
return changeToWords(numb, true);
}
public String changeNumericToWords(String numb)
{
return changeToWords(numb, false);
}
public String changeCurrencyToWords(double numb)
{
return changeToWords(numb.ToString(), true);
}
private String changeToWords(String numb, bool isCurrency)
{
String val = "", wholeNo = numb, points = "", andStr = "", pointStr="";
String endStr = (isCurrency) ? ("Only") : ("");
try
{
int decimalPlace = numb.IndexOf(".");
if (decimalPlace > 0)
{
wholeNo = numb.Substring(0, decimalPlace);
points = numb.Substring(decimalPlace+1);
if (Convert.ToInt32(points) > 0)
{
andStr = (isCurrency)?("and"):("point");// just to separate whole numbers from points/cents
endStr = (isCurrency) ? ("Cents "+endStr) : ("");
pointStr = translateCents(points);
}
}
val = String.Format("{0} {1}{2} {3}",translateWholeNumber(wholeNo).Trim(),andStr,pointStr,endStr);
}
catch { ;}
return val;
}
private String translateWholeNumber(String number)
{
string word = "";
try
{
bool beginsZero = false;//tests for 0XX
bool isDone = false;//test if already translated
double dblAmt = (Convert.ToDouble(number));
//if ((dblAmt > 0) && number.StartsWith("0"))
if (dblAmt > 0)
{//test for zero or digit zero in a nuemric
beginsZero = number.StartsWith("0");
int numDigits = number.Length;
int pos = 0;//store digit grouping
String place = "";//digit grouping name:hundres,thousand,etc...
switch (numDigits)
{
case 1://ones' range
word = ones(number);
isDone = true;
break;
case 2://tens' range
word = tens(number);
isDone = true;
break;
case 3://hundreds' range
pos = (numDigits % 3) + 1;
place = " Hundred ";
break;
case 4://thousands' range
case 5:
case 6:
pos = (numDigits % 4) + 1;
place = " Thousand ";
break;
case 7://millions' range
case 8:
case 9:
pos = (numDigits % 7) + 1;
place = " Million ";
break;
case 10://Billions's range
pos = (numDigits % 10) + 1;
place = " Billion ";
break;
//add extra case options for anything above Billion...
default:
isDone = true;
break;
}
if (!isDone)
{//if transalation is not done, continue...(Recursion comes in now!!)
word = translateWholeNumber(number.Substring(0, pos)) + place + translateWholeNumber(number.Substring(pos));
//check for trailing zeros
if (beginsZero) word = " and " + word.Trim();
}
//ignore digit grouping names
if (word.Trim().Equals(place.Trim())) word = "";
}
}
catch { ;}
return word.Trim();
}
private String tens(String digit)
{
int digt = Convert.ToInt32(digit);
String name = null;
switch (digt)
{
case 10:
name = "Ten";
break;
case 11:
name = "Eleven";
break;
case 12:
name = "Twelve";
break;
case 13:
name = "Thirteen";
break;
case 14:
name = "Fourteen";
break;
case 15:
name = "Fifteen";
break;
case 16:
name = "Sixteen";
break;
case 17:
name = "Seventeen";
break;
case 18:
name = "Eighteen";
break;
case 19:
name = "Nineteen";
break;
case 20:
name = "Twenty";
break;
case 30:
name = "Thirty";
break;
case 40:
name = "Fourty";
break;
case 50:
name = "Fifty";
break;
case 60:
name = "Sixty";
break;
case 70:
name = "Seventy";
break;
case 80:
name = "Eighty";
break;
case 90:
name = "Ninety";
break;
default:
if (digt > 0)
{
name = tens(digit.Substring(0, 1) + "0") + " " + ones(digit.Substring(1));
}
break;
}
return name;
}
private String ones(String digit)
{
int digt = Convert.ToInt32(digit);
String name = "";
switch (digt)
{
case 1:
name = "One";
break;
case 2:
name = "Two";
break;
case 3:
name = "Three";
break;
case 4:
name = "Four";
break;
case 5:
name = "Five";
break;
case 6:
name = "Six";
break;
case 7:
name = "Seven";
break;
case 8:
name = "Eight";
break;
case 9:
name = "Nine";
break;
}
return name;
}
private String translateCents(String cents)
{
String cts = "", digit = "", engOne = "";
for (int i = 0; i < cents.Length; i++)
{
digit = cents[i].ToString();
if (digit.Equals("0"))
{
engOne = "Zero";
}
else
{
engOne = ones(digit);
}
cts += " " + engOne;
}
return cts;
}
}
}

License

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

Share

About the Author

Fred.B
Web Developer
Uganda Uganda
No Biography provided

You may also be interested in...

Comments and Discussions

 
QuestionCode error Pin
Member 1141776817-Jan-16 19:33
memberMember 1141776817-Jan-16 19:33 
Generalerror in conversion Pin
Tshencho4-Jan-16 17:31
memberTshencho4-Jan-16 17:31 
QuestionThanks for you help!!! Pin
Quoclki6-Sep-15 17:24
memberQuoclki6-Sep-15 17:24 
Questionwrong output Pin
Member 1040915822-May-15 20:51
memberMember 1040915822-May-15 20:51 
Questionthank u fred Pin
srigates24-Apr-13 20:17
membersrigates24-Apr-13 20:17 
QuestionHere is a VB.NET one that does the same thing in 23 lines. enjoy! Pin
Member 389485415-Mar-13 8:38
memberMember 389485415-Mar-13 8:38 
GeneralMy vote of 1 Pin
Sergey Alexandrovich Kryukov25-Jan-13 9:16
mvpSergey Alexandrovich Kryukov25-Jan-13 9:16 
QuestionError Pin
lakshantha25-Oct-12 18:14
memberlakshantha25-Oct-12 18:14 
GeneralConversion Error Pin
JL Ruatpuia13-Aug-12 9:35
memberJL Ruatpuia13-Aug-12 9:35 
QuestionGood work Pin
Menon Santosh12-Oct-11 19:51
memberMenon Santosh12-Oct-11 19:51 
AnswerRe: Good work Pin
vijay_dahite3-Oct-15 18:21
membervijay_dahite3-Oct-15 18:21 
GeneralFails on 1020 Pin
dhilsen3020-Aug-10 19:08
memberdhilsen3020-Aug-10 19:08 
GeneralRe: Fails on 1020 Pin
JL Ruatpuia13-Aug-12 9:37
memberJL Ruatpuia13-Aug-12 9:37 
GeneralFormatting Code Pin
KunalChowdhury21-Jul-10 8:26
mentorKunalChowdhury21-Jul-10 8:26 
GeneralThanks mate Pin
dm7920-Jun-10 16:05
memberdm7920-Jun-10 16:05 
GeneralFails on 10020 Pin
arun_kvm18-Nov-09 18:05
memberarun_kvm18-Nov-09 18:05 
GeneralWARNING: FAILS ON 10001, 1000001 Pin
fix_a_fix3-Jun-08 3:28
memberfix_a_fix3-Jun-08 3:28 
GeneralSimpler solution Pin
marschills31-Jan-08 4:19
membermarschills31-Jan-08 4:19 
Generalgreat Pin
amirmuthu17-Oct-07 4:47
memberamirmuthu17-Oct-07 4:47 
GeneralHack for Negative Numbers Pin
sandman_68130-Jul-07 15:47
membersandman_68130-Jul-07 15:47 
GeneralNice One Pin
Boipelo27-Mar-07 0:53
memberBoipelo27-Mar-07 0:53 
Generalthanks Bill Pin
teligaurav9-Feb-07 2:05
memberteligaurav9-Feb-07 2:05 
GeneralCheck this Out Pin
Bill Pierce11-Oct-06 9:48
memberBill Pierce11-Oct-06 9:48 
GeneralRe: Check this Out Pin
fbyk11-Oct-06 21:58
memberfbyk11-Oct-06 21:58 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.160525.2 | Last Updated 11 Oct 2006
Article Copyright 2006 by Fred.B
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid