Ram Ram Friends
Well there are many different great algos (easy & small) to achieve this.
Below is the one of them.
Following is the sample code in C#.Net to convert number into words
Since, I am Indian, so as per Indian requirement, I made this program to suit our basic needs,
But as this algo is very clear & easy to understand, you can convert it very easily by making very few changes to suit your culture/needs.
This algo basically more focus on using strings (very less Mathematical formulas/tricks) to achieve the goal, this make it more simpler & easy to convert it into any programming language.
- using System;
- namespace ArunKakkarConsoleApp1
- {
- public class AK_NumberToWords
- {
- public string GetDigitInWords(int i)
- {
- if (i < 1)
- return "";//"zero";
- string s="";
- if (i.ToString().Length > 2)
- {
- i = Convert.ToInt32(i.ToString().Substring(i.ToString().Length - 2, 2));
- }
- string[] s1 = new string[] { "", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen" };
- string[] s2 = new string[] { "","", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety" };
- string s3, s4;
- if (i < 20)
- s = s1[i];
- else
- {
- s3 = i.ToString().Substring(0, 1);
- s = s2[Convert.ToInt32(s3)];
- s4 = i.ToString().Substring(1, 1);
- s = s + " " + s1[Convert.ToInt32(s4)];
- }
- /*switch (i) //you can use this switch statement if u don't like to use array
- {
- case 1:
- s = "one";
- break;
- case 2:
- s = "two";
- break;
- case 3:
- s = "three";
- break;
- case 4:
- s = "four";
- break;
- case 5:
- s = "five";
- break;
- case 6:
- s = "six";
- break;
- case 7:
- s = "seven";
- break;
- case 8:
- s = "eight";
- break;
- case 9:
- s = "nine";
- break;
- case 10:
- s = "ten";
- break;
- case 11:
- s = "eleven";
- break;
- case 12:
- s = "twelve";
- break;
- case 13:
- s = "thirteen";
- break;
- case 14:
- s = "fourteen";
- break;
- case 15:
- s = "fifteen";
- break;
- case 16:
- s = "sixteen";
- break;
- case 17:
- s = "seventeen";
- break;
- case 18:
- s = "eighteen";
- break;
- case 19:
- s = "nineteen";
- break;
- default:
- string s2;
- s2 = i.ToString().Substring(0, 1);
- switch (Convert.ToInt32(s2))
- {
- case 2:
- s = "twenty";
- break;
- case 3:
- s = "thirty";
- break;
- case 4:
- s = "fourty";
- break;
- case 5:
- s = "fifty";
- break;
- case 6:
- s = "sixty";
- break;
- case 7:
- s = "seventy";
- break;
- case 8:
- s = "eighty";
- break;
- case 9:
- s = "ninety";
- break;
- }
- s2 = i.ToString().Substring(1, 1);
- s = s + " " + this.GetDigitInWords(Convert.ToInt32(s2));
- break;
- }*/
- return s;
- }
- public string GetNoInWords(double d)
- {
- string s="";
- //19,23,45,67,89,12,34,56,789.87
- //Rs ....., 67 Kharab,89 Arab,12 crore, 34 lacs, 56 thousands,7 hundred, 89 and 87 paise only
- //67,89,12,34,56,789.87
- //Rs 67 Kharab,89 Arab,12 crore, 34 lacsh, 56 thousands,7 hundred, 89 and 87 paise only
- string sPaise,sTens, sHundred, sThousand, sLacs, sCrore, sArab, sKharab, sLong;
- int i;
- sLong = ((long)d).ToString();
- if (sLong.Length > 13)
- {
- //s = "Out of range value and maximum value is= ";
- sLong = sLong.Substring(sLong.Length - 13, 13);
- }
- if(sLong.Length <13)
- sLong =sLong.PadLeft(13,'0');
- //Console.WriteLine("\nslong=" + sLong);
- sKharab = sLong.Substring(0, 2); //Kharab
- sArab = sLong.Substring(2, 2); //arab
- sCrore = sLong.Substring(4, 2); //Crore
- sLacs = sLong.Substring(6, 2); //Lacs
- sThousand = sLong.Substring(8, 2); //thousands
- sHundred = sLong.Substring(10, 1); //Hundred
- sTens = sLong.Substring(11, 2); //Tens
- if (d.ToString().Contains("."))
- sPaise = d.ToString().Substring(d.ToString().IndexOf('.')+1);
- else
- sPaise = "0";
- if (sPaise.Length>2)
- sPaise = sPaise.Substring(0, 2);
- i = Convert.ToInt16(sKharab);
- if (i > 0)
- s = this.GetDigitInWords(i) + ((i>1)? " Kharabs " : " Kharab ");
- i = Convert.ToInt16(sArab);
- if (i > 0)
- s += this.GetDigitInWords(i) + ((i>1)?" Arabs " : " Arab ");
- i = Convert.ToInt16(sCrore);
- if (i > 0)
- s += this.GetDigitInWords(i) + ((i>1)?" Crores " : " Crore ");
- i = Convert.ToInt16(sLacs);
- if (i > 0)
- s += this.GetDigitInWords(i) + ((i>1)? " Lacs ": " Lac ");
- i = Convert.ToInt16(sThousand);
- if (i > 0)
- s += this.GetDigitInWords(i) + ((i>1)? " Thousands " : " Thousand ");
- i = Convert.ToInt16(sHundred);
- if (i > 0)
- s += this.GetDigitInWords(i) +((i>1)? " Hundreds ": " Hundred ");
- i=Convert.ToInt16(sTens);
- if (i > 0)
- s += this.GetDigitInWords(i) + " ";
- i = Convert.ToInt16(sPaise);
- if (i > 0)
- s += " and " + this.GetDigitInWords(i) + ((i>0)? " Paises ": " Paise ");
- s += "only";
- return s;
- }//public string GetNoInWords(double d)
//Below is using Math logic
//I tried to give example at almost each step to understand logic
- public String GetDigitInWordsByMath(int i)
- {
- string s="";
- int j, k;
- string[] s1 = new string[] { "", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "tweleve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen" };
- string[] s2 = new string[] { "", "", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety" };
- if (i < 20)
- s = s1[i];
- else
- {
- j = i / 10; //tens digit
- k = i % 10; //unit digit
- s = s2[j] + " " + s1[k];
- }
- return s;
- }
- public string GetNumInWordsByMath(double d)
- {
- //19,23,45,67,89,12,34,56,789.87
- //Rs ....., 67 Kharab,89 Arab,12 crore, 34 lacs, 56 thousands,7 hundred, 89 and 87 paise only
- //67,89,12,34,56,789.87
- //Rs 67 Kharab,89 Arab,12 crore, 34 lacs, 56 thousands,7 hundred, 89 and 87 paise only
- string s="";
- long myNum;
- int iPaise, iDigit, iHundred, iThousand, iLac, iCrore, iArab, iKharab;
- //let d=1234567898765.43
- // 25%10=5 (return Remainder) applicable in Integer only
- // 25/10=2 (return Quotient) applicable in Integer only
- myNum = (long)(d*100); //myNum=1234567898765.43*100 =123456789876543
- iPaise = (int)(myNum % 100); //iPaise=123456789876543%100 =43
- myNum = myNum / 100; //myNum=123456789876543/100 =1234567898765
- iDigit = (int)(myNum % 100); //iDigit=1234567898765 %100 =65
- myNum = myNum / 100; //myNum =1234567898765 /100 =12345678987
- iHundred =(int)( myNum % 10); //iHundred=12345678987 %10 =7
- myNum = myNum / 10; //myNum =12345678987/10 =1234567898
- iThousand = (int)(myNum % 100); //iThousand=1234567898%100 =98
- myNum = myNum / 100; //myNum=1234567898/100 =12345678
- iLac = (int)(myNum % 100); //iLac=12345678%100 =78
- myNum = myNum / 100; //myNum=12345678/100 =123456
- iCrore = (int)(myNum % 100); //iCrore=123456%100 =56
- myNum = myNum / 100; //myNum=123456/100 =1234
- iArab = (int)(myNum % 100); //iArab=1234%100 =34
- myNum = myNum / 100; //myNum=1234/100 =12
- iKharab = (int)(myNum % 100); //iKharab=12%100 =12
- myNum = myNum / 100; //myNum=12/100 =0
- if (iKharab > 0)
- s = this.GetDigitInWordsByMath(iKharab) + ((iKharab > 1) ? " Kharabs " : " Kharab ");
- if (iArab > 0)
- s += this.GetDigitInWordsByMath(iArab) + ((iArab > 1) ? " Arabs " : " Arab ");
- if (iCrore > 0)
- s += this.GetDigitInWordsByMath(iCrore) + ((iCrore > 1) ? " Crores " : " Crore ");
- if (iLac > 0)
- s += this.GetDigitInWordsByMath(iLac) + ((iLac > 1) ? " Lacs " : " Lac ");
- if (iThousand > 0)
- s += this.GetDigitInWordsByMath(iThousand) + ((iThousand > 1) ? " Thousands " : " Thousand ");
- if (iHundred > 0)
- s += this.GetDigitInWordsByMath(iHundred) + ((iHundred > 1) ? " Hundreds " : " Hundred ");
- if (iDigit > 0)
- s += this.GetDigitInWordsByMath(iDigit) + " ";
- if (iPaise > 0)
- s += "and " + this.GetDigitInWordsByMath(iPaise) + ((iPaise > 1) ? " Paises " : " Paise ");
- s= "Rs " + s + "only";
- return s;
- }
- } //class
- class Program
- {
- static void Main(string[] args)
- {
- AK_NumberToWords nw1 = new AK_NumberToWords();
- string myAns,myVal;
- do
- {
- Console.WriteLine("Please enter 2 digit no to get its value in words");
- myVal = Console.ReadLine();
- Console.WriteLine("{0} in words is={1}", myVal, nw1.GetDigitInWords(Convert.ToInt32(myVal)));
- Console.WriteLine("\nPlease enter any number to get its value in words:");
- myVal = Console.ReadLine();
- Console.WriteLine("{0} in words is=\n {1}", myVal, nw1.GetNoInWords(Convert.ToDouble(myVal)));
- Console.WriteLine("\nPlease enter any number to get its value in words using math logic:");
- myVal = Console.ReadLine();
- Console.WriteLine("{0} in words is=\n {1}", myVal, nw1.GetNumInWordsByMath(Convert.ToDouble(myVal)));
- Console.WriteLine("Do u want to try more?(Y/N)");
- myAns = Console.ReadLine();
- } while (myAns == "Y" || myAns == "y");
- }//class program
- }//namespace
Friends, as I already told, it is just one of many different algo used to convert number into words.
Of course, there are lot of ways to minimize/modify it to get more accuracy, speed, & less memory consumption etc.
I hope it would help you.
Wish you a very happy programming.
Thank you sir, your post helped me a lot....
ReplyDeleteplz check ur logic it giving wrong answer for input 55.50..
ReplyDeletepublic static string NumberToWords(int number)
ReplyDelete{
if (number == 0)
return "zero";
if (number < 0)
return "minus " + NumberToWords(Math.Abs(number));
string words = "";
if ((number / 1000000) > 0)
{
words += NumberToWords(number / 1000000) + " million ";
number %= 1000000;
}
if ((number / 1000) > 0)
{
words += NumberToWords(number / 1000) + " thousand ";
number %= 1000;
}
if ((number / 100) > 0)
{
words += NumberToWords(number / 100) + " hundred ";
number %= 100;
}
if (number > 0)
{
if (words != "")
words += "and ";
var unitsMap = new[] { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen" };
var tensMap = new[] { "zero", "ten", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety" };
if (number < 20)
words += unitsMap[number];
else
{
words += tensMap[number / 10];
if ((number % 10) > 0)
words += "-" + unitsMap[number % 10];
}
}
return words;
}