Converting Amount in Indian Rupess Format

  1. #region AmountInWords  
  2. public string AmountInWords(decimal Num) {  
  3.     string returnValue;  
  4.     //I have created this function for converting amount in indian rupees (INR).  
  5.     //You can manipulate as you wish like decimal setting, Doller (any currency) Prefix.  
  6.   
  7.   
  8.     string strNum;  
  9.     string strNumDec;  
  10.     string StrWord;  
  11.     strNum = Num.ToString();  
  12.   
  13.   
  14.     if (strNum.IndexOf(".") + 1 != 0) {  
  15.         strNumDec = strNum.Substring(strNum.IndexOf(".") + 2 - 1);  
  16.   
  17.   
  18.         if (strNumDec.Length == 1) {  
  19.             strNumDec = strNumDec + "0";  
  20.         }  
  21.         if (strNumDec.Length > 2) {  
  22.             strNumDec = strNumDec.Substring(0, 2);  
  23.         }  
  24.   
  25.   
  26.         strNum = strNum.Substring(0, strNum.IndexOf(".") + 0);  
  27.         StrWord = ((double.Parse(strNum) == 1) ? " Rupee " : " Rupees ") + NumToWord((decimal)(double.Parse(strNum))) + ((double.Parse(strNumDec) > 0) ? (" and Paise" + cWord3((decimal)(double.Parse(strNumDec)))) : "");  
  28.     } else {  
  29.         StrWord = ((double.Parse(strNum) == 1) ? " Rupee " : " Rupees ") + NumToWord((decimal)(double.Parse(strNum)));  
  30.     }  
  31.     returnValue = StrWord + " Only";  
  32.     return returnValue;  
  33. }  
  34. static public string NumToWord(decimal Num) {  
  35.     string returnValue;  
  36.   
  37.   
  38.     //I divided this function in two part.  
  39.     //1. Three or less digit number.  
  40.     //2. more than three digit number.  
  41.     string strNum;  
  42.     string StrWord;  
  43.     strNum = Num.ToString();  
  44.   
  45.   
  46.     if (strNum.Length <= 3) {  
  47.         StrWord = cWord3((decimal)(double.Parse(strNum)));  
  48.     } else {  
  49.         StrWord = cWordG3((decimal)(double.Parse(strNum.Substring(0, strNum.Length - 3)))) + " " + cWord3((decimal)(double.Parse(strNum.Substring(strNum.Length - 2 - 1))));  
  50.     }  
  51.     returnValue = StrWord;  
  52.     return returnValue;  
  53. }  
  54. static public string cWordG3(decimal Num) {  
  55.     string returnValue;  
  56.     //2. more than three digit number.  
  57.     string strNum = "";  
  58.     string StrWord = "";  
  59.     string readNum = "";  
  60.     strNum = Num.ToString();  
  61.     if (strNum.Length % 2 != 0) {  
  62.         readNum = System.Convert.ToString(double.Parse(strNum.Substring(0, 1)));  
  63.         if (readNum != "0") {  
  64.             StrWord = retWord(decimal.Parse(readNum));  
  65.             readNum = System.Convert.ToString(double.Parse("1" + strReplicate("0", strNum.Length - 1) + "000"));  
  66.             StrWord = StrWord + " " + retWord(decimal.Parse(readNum));  
  67.         }  
  68.         strNum = strNum.Substring(1);  
  69.     }  
  70.     while (!System.Convert.ToBoolean(strNum.Length == 0)) {  
  71.         readNum = System.Convert.ToString(double.Parse(strNum.Substring(0, 2)));  
  72.         if (readNum != "0") {  
  73.             StrWord = StrWord + " " + cWord3(decimal.Parse(readNum));  
  74.             readNum = System.Convert.ToString(double.Parse("1" + strReplicate("0", strNum.Length - 2) + "000"));  
  75.             StrWord = StrWord + " " + retWord(decimal.Parse(readNum));  
  76.         }  
  77.         strNum = strNum.Substring(2);  
  78.     }  
  79.     returnValue = StrWord;  
  80.     return returnValue;  
  81. }  
  82. static public string cWord3(decimal Num) {  
  83.     string returnValue;  
  84.     //1. Three or less digit number.  
  85.     string strNum = "";  
  86.     string StrWord = "";  
  87.     string readNum = "";  
  88.     if (Num < 0) {  
  89.         Num = Num * -1;  
  90.     }  
  91.     strNum = Num.ToString();  
  92.   
  93.   
  94.     if (strNum.Length == 3) {  
  95.         readNum = System.Convert.ToString(double.Parse(strNum.Substring(0, 1)));  
  96.         StrWord = retWord(decimal.Parse(readNum)) + " Hundred";  
  97.         strNum = strNum.Substring(1, strNum.Length - 1);  
  98.     }  
  99.   
  100.   
  101.     if (strNum.Length <= 2) {  
  102.         if (double.Parse(strNum) >= 0 && double.Parse(strNum) <= 20) {  
  103.             StrWord = StrWord + " " + retWord((decimal)(double.Parse(strNum)));  
  104.         } else {  
  105.             StrWord = StrWord + " " + retWord((decimal)(System.Convert.ToDouble(strNum.Substring(0, 1) + "0"))) + " " + retWord((decimal)(double.Parse(strNum.Substring(1, 1))));  
  106.         }  
  107.     }  
  108.   
  109.   
  110.     strNum = Num.ToString();  
  111.     returnValue = StrWord;  
  112.     return returnValue;  
  113. }  
  114. static public string retWord(decimal Num) {  
  115.     string returnValue;  
  116.     //This two dimensional array store the primary word convertion of number.  
  117.     returnValue = "";  
  118.     object[, ] ArrWordList = new object[, ] {  
  119.         {  
  120.             0, ""  
  121.         }, {  
  122.             1, "One"  
  123.         }, {  
  124.             2, "Two"  
  125.         }, {  
  126.             3, "Three"  
  127.         }, {  
  128.             4, "Four"  
  129.         }, {  
  130.             5, "Five"  
  131.         }, {  
  132.             6, "Six"  
  133.         }, {  
  134.             7, "Seven"  
  135.         }, {  
  136.             8, "Eight"  
  137.         }, {  
  138.             9, "Nine"  
  139.         }, {  
  140.             10, "Ten"  
  141.         }, {  
  142.             11, "Eleven"  
  143.         }, {  
  144.             12, "Twelve"  
  145.         }, {  
  146.             13, "Thirteen"  
  147.         }, {  
  148.             14, "Fourteen"  
  149.         }, {  
  150.             15, "Fifteen"  
  151.         }, {  
  152.             16, "Sixteen"  
  153.         }, {  
  154.             17, "Seventeen"  
  155.         }, {  
  156.             18, "Eighteen"  
  157.         }, {  
  158.             19, "Nineteen"  
  159.         }, {  
  160.             20, "Twenty"  
  161.         }, {  
  162.             30, "Thirty"  
  163.         }, {  
  164.             40, "Forty"  
  165.         }, {  
  166.             50, "Fifty"  
  167.         }, {  
  168.             60, "Sixty"  
  169.         }, {  
  170.             70, "Seventy"  
  171.         }, {  
  172.             80, "Eighty"  
  173.         }, {  
  174.             90, "Ninety"  
  175.         }, {  
  176.             100, "Hundred"  
  177.         }, {  
  178.             1000, "Thousand"  
  179.         }, {  
  180.             100000, "Lakh"  
  181.         }, {  
  182.             10000000, "Crore"  
  183.         }, {  
  184.             100000000, "Ten Crore"  
  185.         }, {  
  186.             1000000000, "Arab"  
  187.         }, {  
  188.             10000000000, "Ten Arab"  
  189.         }, {  
  190.             100000000000, "Kharab"  
  191.         }  
  192.     };  
  193.   
  194.   
  195.     int i;  
  196.     for (i = 0; i <= (ArrWordList.Length - 1); i++) {  
  197.         if (Num == System.Convert.ToDecimal(ArrWordList[i, 0])) {  
  198.             returnValue = (string)(ArrWordList[i, 1]);  
  199.             break;  
  200.         }  
  201.     }  
  202.     return returnValue;  
  203. }  
  204. static public string strReplicate(string str, int intD) {  
  205.     string returnValue;  
  206.     //This fucntion padded "0" after the number to evaluate hundred, thousand and on....  
  207.     //using this function you can replicate any Charactor with given string.  
  208.     int i;  
  209.     returnValue = "";  
  210.     for (i = 1; i <= intD; i++) {  
  211.         returnValue = returnValue + str;  
  212.     }  
  213.     return returnValue;  
  214. }#endregion