Sei sulla pagina 1di 6

using using using using

System; System.Collections.Generic; System.Linq; System.Text;

namespace WindowsFormsApplication2 { public class Numeric { 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", "fiv e", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fou rteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen" }; string[] s2 = new string[] { "", "", "twenty", "thirty", "forty", "f ifty", "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 t o 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 hun dred, 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, 8 9 and 87 paise only string sPaise, sTens, sHundred, sThousand, sLacs, sCrore, sArab, sKh arab, 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 " : " Thou sand "); i = Convert.ToInt16(sHundred); if (i > 0) s += this.GetDigitInWords(i) + ((i > 1) ? " Hundreds " : " Hundr ed "); 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", "fiv e", "six", "seven", "eight", "nine", "ten", "eleven", "tweleve", "thirteen", "fo urteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen" }; string[] s2 = new string[] { "", "", "twenty", "thirty", "forty", "f ifty", "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 hun dred, 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, iKhara b; //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 =1234567898765 43 iPaise = (int)(myNum % 100); //iPaise=123456789876543%100 =43 myNum = myNum / 100; //myNum=123456789876543/100 =12345678987 65 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 myNum = myNum / 100; //myNum=12345678/100 iCrore = (int)(myNum % 100); //iCrore=123456%100 myNum = myNum / 100; //myNum=123456/100 iArab = (int)(myNum % 100); //iArab=1234%100 myNum = myNum / 100; //myNum=1234/100 iKharab = (int)(myNum % 100); //iKharab=12%100 myNum = myNum / 100; //myNum=12/100 =78 =123456 =56 =1234 =34 =12 =12 =0

if (iKharab > 0) s = this.GetDigitInWordsByMath(iKharab) + ((iKharab > 1) ? " Kha rabs " : " Kharab "); if (iArab > 0) s += this.GetDigitInWordsByMath(iArab) + ((iArab > 1) ? " Arabs " : " Arab "); if (iCrore > 0) s += this.GetDigitInWordsByMath(iCrore) + ((iCrore > 1) ? " Cror es " : " 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 }

Potrebbero piacerti anche