Introduction
Recently in my current project, I encountered the requirement to generate a customer invoice with the total amount converted into its verbal representation;
Example. 200is "Two Hundred"
5150.20 is "Five thousand one hundred fifty rupees and twenty paise.
Before I wrote my own code, I first googled it, but unfortunately (or maybe I am a bad Googler), I didn't get any solution that could convert numbers into words (Indian Format), so I decided to write my own solution rather than wasting time on Google, I had a basic idea of how to do it, and after doing a lot of code-reengineering, I finally ended up with working code.
Example
using System;
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Do You wish to continue Y/N");
while (Console.ReadLine().ToLower() != "n")
{
Console.WriteLine("Enter Number");
string s = Console.ReadLine();
ConvertMyword(int.Parse(s));
Console.WriteLine("Do You wish to continue Y/N");
}
}
static void ConvertMyword(int number)
{
int flag = 0;
int lflag = 0;
string words = String.Empty;
string[] places = { "ones", "ten", "hundred", "thousand", "ten thousand", "lacs", "tenlacs", "crore", "tencrore", "Billon" };
string rawnumber = number.ToString();
char[] a = rawnumber.ToCharArray();
Array.Reverse(a);
if (a.Length >= 2)
{
for (int i = a.Length - 1; i >= 0; i--)
{
if (i % 2 == 0 && i > 2)
{
if (int.Parse(a[i].ToString()) > 1)
{
if (int.Parse(a[i - 1].ToString()) == 0)
{
words = words + getNumberStringty(int.Parse(a[i].ToString())) + " " + places[i - 1] + " ";
}
else
{
words = words + getNumberStringty(int.Parse(a[i].ToString())) + " ";
}
}
else if (int.Parse(a[i].ToString()) == 1)
{
if (int.Parse(a[i - 1].ToString()) == 0)
{
words = words + "Ten" + " ";
}
else
{
words = words + getNumberStringteen(int.Parse(a[i - 1].ToString())) + " ";
}
flag = 1;
}
}
else
{
if (i == 1 || i == 0)
{
if (int.Parse(a[i].ToString()) > 1)
{
words = words + getNumberStringty(int.Parse(a[i].ToString())) + " " + getNumberString(int.Parse(a[0].ToString())) + " ";
break;
}
else if (int.Parse(a[i].ToString()) == 1)
{
if (int.Parse(a[i - 1].ToString()) == 0)
{
words = words + "Ten" + " ";
}
else
{
words = words + getNumberStringteen(int.Parse(a[i - 1].ToString())) + " ";
}
break;
}
else if (int.Parse(a[i - 1].ToString()) != 0)
{
words = words + getNumberString(int.Parse(a[i - 1].ToString())) + " ";
break;
}
else
{
break;
}
}
else
{
if (flag == 0)
{
for (int l = i; l >= 0; l--)
{
if (int.Parse(a[l].ToString()) != 0)
{
lflag = 1;
}
}
if (lflag == 1 && int.Parse(a[i].ToString()) != 0)
{
words = words + getNumberString(int.Parse(a[i].ToString())) + " " + places[i] + " ";
lflag = 0;
}
else if (lflag == 0)
{
lflag = 0;
break;
}
}
else
{
words = words + " " + places[i] + " ";
flag = 0;
}
}
}
}
}
else
{
words = getNumberString(int.Parse(a[0].ToString()));
}
Console.WriteLine(words);
}
static string getNumberString(int num)
{
string Word = String.Empty;
switch (num)
{
case 1:
Word = "one";
break;
case 2:
Word = "two";
break;
case 3:
Word = "three";
break;
case 4:
Word = "four";
break;
case 5:
Word = "five";
break;
case 6:
Word = "six";
break;
case 7:
Word = "seven";
break;
case 8:
Word = "eight";
break;
case 9:
Word = "nine";
break;
}
return Word;
}
static string getNumberStringty(int num)
{
string Word = String.Empty;
switch (num)
{
case 2:
Word = "twenty";
break;
case 3:
Word = "thirty";
break;
case 4:
Word = "forty";
break;
case 5:
Word = "fifty";
break;
case 6:
Word = "sixty";
break;
case 7:
Word = "seventy";
break;
case 8:
Word = "eighty";
break;
case 9:
Word = "ninety";
break;
}
return Word;
}
static string getNumberStringteen(int num)
{
string Word = String.Empty;
switch (num)
{
case 1:
Word = "eleven";
break;
case 2:
Word = "twelve";
break;
case 3:
Word = "thirteen";
break;
case 4:
Word = "fourteen";
break;
case 5:
Word = "fifteen";
break;
case 6:
Word = "sixteen";
break;
case 7:
Word = "seventeen";
break;
case 8:
Word = "eighteen";
break;
case 9:
Word = "nineteen";
break;
}
return Word;
}
}
}

Nagashyam KumarPosted Jan 28, 2013, 7:27 AM
Below the better way of coding, reduces 2/3 of code and many if condition string[] once = new string[] { "", "ONE", "TWO", "THREE", "FOUR", "FIVE", "SIX", "SEVEN", "EIGHT", "NINE"}; string[] tens = new string[] { "", "", "TWENTY", "THIRTY", "FOUTY", "FIFTY", "SIXTY", "SEVENTY", "EIGHTY", "NINTY", "HUNDRED" }; string [] tweentys = new string[] {"TEN", "ELEVEN", "TWELVE", "THIRTEEN", "FOURTEEN", "FIFTEEN", "SIXTEEN", "SEVEENTEEN", "EIGHTEEN", "NINETEEN"}; string[] hundredes = new string[] {"", "THOUSAND", "LAKHS", "CRORES","BILLION"}; private string ConvetToWord(string number) { ArrayList arrayList = new ArrayList(); int decimalPlaces = 3; string ModifiedNumber = number; while (ModifiedNumber != string.Empty) { string strTempNo = string.Empty; if (ModifiedNumber.Length <= decimalPlaces) { strTempNo = ModifiedNumber; ModifiedNumber = string.Empty; } else { strTempNo = ModifiedNumber.Substring((ModifiedNumber.Length - decimalPlaces), decimalPlaces); ModifiedNumber = ModifiedNumber.Substring(0, (ModifiedNumber.Length - decimalPlaces)); decimalPlaces = 2; } arrayList.Add(strTempNo); } string words = string.Empty; for (int i = 0; i < arrayList.Count; i++) { char[] Number = arrayList[i].ToString().ToCharArray(); Array.Reverse(Number); string tempWord = string.Empty; for (int j = 0; j < Number.Length; j++) { int charCount = int.Parse(Number[j].ToString()); switch (j) { case 0: tempWord = once[charCount] + " "+ tempWord ; break; case 1: if (charCount == 1) { tempWord = tweentys[int.Parse(Number[0].ToString())]; } else { tempWord = tens[charCount] + " " + tempWord; } break; case 2: if (charCount != 0) { tempWord = once[charCount] + " HUNDRED" + " " + tempWord; } break; } } if (i > 0 && tempWord.Trim() != string.Empty) { words = hundredes[i] + words; } words = tempWord + " " + words; } return words; }
Ahmar HusainPosted Jan 28, 2013, 1:26 AM
Vithal i just checked your article it is great but u left a very basic thing you are splitting the input string on '.' if user will enter integer value then it will throw error.
Ahmar HusainPosted Jan 28, 2013, 1:15 AM
Mr Hobs good to read you, No I didn't see any related article in this website , my code will only return verbal representation of numbers if you want to append dollars or rupees in the returned string u can easily do it , I above wrote it is in indian format because here in india we call 1000000 as ten Lacs however in US it is one million or 100000 as one lac but in US I think it is hundred thousand.
Vithal WadjePosted Jan 25, 2013, 1:27 AM
yes sam sir,i have written same kind of article before 3 months ago
Sam HobbsPosted Jan 24, 2013, 8:37 PM
Have you seen the relevant other articles in this web site? I think there is at least one article, and I think more than one. I don't know if they do this for Rupees but I assume any that does Dollars can be modified for Rupees.