Subscribe to Subscribers
Talk to Webmaster Tony John


Resources » .NET programming » General

Convert Currency to English Words


Posted Date:     Category: General    
Author: Member Level: Silver    Points: 10



 


Introduction:


This article will show how to convert the decimal currency to english words.

Scope:


The example in the article has been tested with VS.Net 2005 and VS.Net 2008 running on XP and W2003 clients. Example supports the decimal values upto Quadrillion and is in dollar format. This can be easily modified to any other currecy formats like Indian rupee.

The entire code is posted together and explained through commets. This will make it easier for the reader to copy it.


#region Namespace
using System;
using System.Text;
using System.Text.RegularExpressions;
#endregion

#region Copyright©

#endregion

public class CurrencyTranslator
{
// Array of sting to hold the words from one to nineteen
private string[] _arrayOfOnes = { string.Empty, "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight",
"Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen",
"Nineteen" };
// Array of string to hold the words of tens - 10, 20,.., 90
private string[] _arrayOfTens = { string.Empty , "Ten", "Twenty", "Thirty", "Forty", "Fifty", "Sixty",
"Seventy", "Eighty", "Ninety"};

// Converts the decimal to currency
public string TranslateCurrency(decimal currencyValue)
{
string numericCurrency = currencyValue.ToString().Trim ();
//Check for the max capacity limit of the input
if (numericCurrency.Length > 18)
return "Invalid input format";

string leftValue, decimalWord;
//Right align the charecters with padding of "0" to length the of 18 charecters
if (numericCurrency.IndexOf(".") >= 0)
{
leftValue = numericCurrency.Substring(0, numericCurrency.IndexOf(".")).PadLeft(18, Convert.ToChar("0"));
decimalWord = numericCurrency.Substring(numericCurrency.IndexOf(".") + 1).PadRight(2, Convert.ToChar("0"));
decimalWord = (decimalWord.Length > 2 ? decimalWord.Substring(0, 2) : decimalWord);
}
else
{
leftValue = numericCurrency.PadLeft(18, Convert.ToChar("0"));
decimalWord = "0";
}

string quadrillionWord, trillionWord, billionWord, millionWord, thousandWord, hundredWord;
quadrillionWord = TranslateHundreds(Convert.ToInt32(leftValue.Substring(0, 3))); // One Quadrillion - Number of zeros 15
trillionWord = TranslateHundreds(Convert.ToInt32(leftValue.Substring(3, 3))); // One Trillion - Number of zeros 12
billionWord = TranslateHundreds(Convert.ToInt32(leftValue.Substring(6, 3))); // One Billion - Number of zeros 9
millionWord = TranslateHundreds(Convert.ToInt32(leftValue.Substring(9, 3))); // One Million - Number of zeros 6
thousandWord = TranslateHundreds(Convert.ToInt32(leftValue.Substring(12, 3)));
hundredWord = TranslateHundreds(Convert.ToInt32(leftValue.Substring(15, 3)));
decimalWord = TranslateDecimal(decimalWord);

// Start building the currency
StringBuilder currencyInEnglish = new StringBuilder();
currencyInEnglish.Append((quadrillionWord.Trim() == string.Empty ? string.Empty : quadrillionWord + " Quadrillion "));
currencyInEnglish.Append((trillionWord.Trim() == string.Empty ? string.Empty : trillionWord + " Trillion "));
currencyInEnglish.Append((billionWord.Trim() == string.Empty ? string.Empty : billionWord + " Billion "));
currencyInEnglish.Append((millionWord.Trim() == string.Empty ? string.Empty : millionWord + " Million "));
currencyInEnglish.Append((thousandWord.Trim() == string.Empty ? string.Empty : thousandWord + " Thousand "));
currencyInEnglish.Append((hundredWord.Trim() == string.Empty ? string.Empty : hundredWord));

currencyInEnglish.Append(currencyInEnglish.ToString() == string.Empty ? "Zero Dollars " : " Dollars");
if (currencyInEnglish.ToString().StartsWith("One Dollars"))
{
currencyInEnglish.Replace("One Dollars", "One Dollar");
}
currencyInEnglish.Append((decimalWord == string.Empty ? " and Zero Cents" : " and " + decimalWord + " Cents"));
return currencyInEnglish.ToString();
}


#region Private Functions


private string TranslateHundreds(int value)
{
string result;
// If the value is less than hundred then convert it as tens
if (value <= 99)
{
result = (TranslateTens(value));
}
else
{
result = _arrayOfOnes[Convert.ToInt32(Math.Floor(Convert.ToDecimal(value / 100)))];
// Math.Floor method is used to get the largest integer from the decial value
result += " Hundred ";
// The rest of the decimal is converted into tens
if (value - Math.Floor(Convert.ToDecimal((value / 100) * 100)) == 0)
{
result += string.Empty;
}
else
{
result += string.Empty + TranslateTens(Convert.ToInt32(value - Math.Floor(Convert.ToDecimal((value / 100) * 100))));
}
}
return result;
}


private string TranslateTens(int value)
{
string result;
// If the value is less than 20 then get the word directly from the array
if (value < 20)
{
result = _arrayOfOnes[value];
value = 0;
}
else
{
result = _arrayOfTens[(int)Math.Floor(Convert.ToDecimal(value / 10))];
value -= Convert.ToInt32(Math.Floor(Convert.ToDecimal((value / 10) * 10)));
}
result = result + (_arrayOfOnes[value].Trim() == string.Empty ? string.Empty : "-" + _arrayOfOnes[value]);
return result;
}

// Translates the decimal part of the currency to words
private string TranslateDecimal(string value)
{
string result = string.Empty;
// Check whether the decimal part starts with a zero. Example : 12.05
if (value != "0")
{
if (value.StartsWith("0"))
{
result = "Zero ";
result += _arrayOfOnes[Convert.ToInt32(value.Substring(1, 1))];
}
else
{
result = TranslateTens(Convert.ToInt32(value));
}

}
return result;
}
#endregion
}




- Naison Garvasis Pekkattil






Did you like this resource? Share it with your friends and show your love!


Responses to "Convert Currency to English Words"
Author: Mahesh Raj    07 Jun 2008Member Level: Gold   Points : 1
This is very good information,Continue posting such useful articles.


Author: John Fernandez    08 Jun 2008Member Level: Gold   Points : 1
Very well written Article.Thanks for sharing this information.


Author: Satish Kumar J    12 May 2009Member Level: Gold   Points : 0
Very useful one , keep posting these kind of articles.

Regards,
Satish.



Author: senthilvijay    13 Jun 2009Member Level: Bronze   Points : 1
Hai,
Thanks for your code.
but how to convert the indian Rupees or curency to english words.
PLease Help me with the code,
Regards,
senthilviajy



Author: Prasoon Kumar    15 Jun 2009Member Level: Gold   Points : 2
Hi
Thanks for the code.

Just to answer senthilvijay for his query of

how to convert the indian Rupees or curency to english words.

In any place of the code ,i am not seeing any conversion factor

involved.

Decimal,thousand,Hundred ar all absolute terms for any currency .

So i don't think the code will change for dollars ,euros,or Ruppes.


But I have an idea of extending this code.

We can make it more customizable by not limiting it to english

words but to support many languages by introducing

Localization that is by using Resource file.

Naison , can you give your insight into the same.

Regards

Prasoon.



Feedbacks      

Post Comment:




  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:   Sign In to fill automatically.
    Email: (Will not be published, but required to validate comment)



    Type the numbers and letters shown on the left.


    Next Resource: Bitwise logical OR
    Previous Resource: New features in the SQL for the COMMING Future
    Return to Resources
    Post New Resource
    Category: General


    Post resources and earn money!
     
    More Resources
    Popular Tags   Tag posting guidelines   Search Tags  
    Currency Convertor  .  Currency to English Words  .  Dollar to English  .  Currency to Words  .  Currency to Sentance  .  



    Follow us on Twitter: https://twitter.com/dotnetspider

    Active Members
    Today
      Last 7 Daysmore...

      Awards & Gifts
      Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India
      2005 - 2012 All Rights Reserved.
      .NET and other trademarks mentioned in this site belong to Microsoft and other respective trademark owners.
      Articles, tutorials and all other content offered here is for educational purpose only.
      We are not associated with Microsoft or its partners.