How to convert a Number to Currency using C#

In this article I've tried to give code snippet with which a numeric value can be converted into currency by using C# programming language. Basically this function can be used to convert the value into currency in word. Put a currency symbol just before function.

It is essential to convert the any currency value to value in word in a ERP solution or any financial system. Here I've given code snippet where any dolar value (limit to 9 digit) can be converted into value in word. I've used the following function to perform the whole task.

1. ConvertNumberToWord(String MyNumber)
2. ConvertSingleDigit(String MyNumber)
3. ConvertDoubleDigit(String MyNumber)
4. ConvertHundreds(String MyNumber)
5. ConvertThousands(String MyNumber)
6. ConvertCrore(String MyNumber)

After receiving numeric value all coma (,) if it remain, has been removed and decimal part has been separated into two part. Only two decimal value is acceptable. US dollar and cent has been used as a currency.

``public string ConvertNumberToWord(string MyNumber)        {            string DecimalPart, DecimalPartWord, RealPart, RealPartWord;            int k, RealPartLength;            //string Place2=" Thousand ", Place3=" Lakh ", Place4=" Crore ", Place5=" Billion ", Place6=" Trillion ";            MyNumber = MyNumber.Trim();            //////////////////////////////// Split MyNumber into RealPart and DecimalPart ///////////////////////////////////////            k = MyNumber.IndexOf(".");            if (k< 0)            {                DecimalPart = ""; //There is no decimal part                RealPart = MyNumber.Replace(",",""); //Remove all coma if any            }            else if (k == 0)            {                DecimalPart = MyNumber.Substring(k, 2);                RealPart = ""; //There is no real part            }            else            {                DecimalPart = MyNumber.Substring(k+1, 2);                RealPart = MyNumber.Remove(k);                RealPart = RealPart.Replace(",", ""); //Remove all coma if any            }            ////////////////////////////////// Convert DecimalPart into DecimalPartWord ///////////////////////////////////////////////////            if (DecimalPart != "")                DecimalPartWord = ConvertDoubleDigit(DecimalPart);            else                DecimalPartWord = "";            ////////////////////////////////////// Convert RealPart into Word /////////////////////////////////////////////////            RealPartLength=RealPart.Length;            if (RealPartLength == 1)                RealPartWord = ConvertSingleDigit(RealPart);            else if (RealPartLength == 2)                RealPartWord = ConvertDoubleDigit(RealPart);            else if (RealPartLength == 3)                RealPartWord = ConvertHundred(RealPart);            else if (RealPartLength > 3 && RealPartLength < 6)                RealPartWord = ConvertThousand(RealPart, RealPartLength);            else if (RealPartLength > 5 && RealPartLength < 8)                RealPartWord = ConvertLakh(RealPart, RealPartLength);            else                RealPartWord = ConvertCrore(RealPart,RealPartLength);               // ////////////////Return /////////////////////////            if (RealPartWord == "" && DecimalPartWord == "")                return "";            else if(RealPartWord!="" && DecimalPartWord!="")                return "USD "+RealPartWord + " and Cent " + DecimalPartWord;            else if(RealPartWord!="" && DecimalPartWord=="")                return "USD " + RealPartWord;            else                return " Cent " + DecimalPartWord;        }        ///////////////////////////////////////// Convert Hundred Part into Word ///////////////////////////////////////////////        private string ConvertHundred(string MyNumber)        {            string Result;            if (MyNumber.Remove(1, 2) != "0")            {                if (ConvertSingleDigit(MyNumber.Remove(1, 2)) == "")                    Result = "";                else                    Result = ConvertSingleDigit(MyNumber.Remove(1, 2)) + " Hundred ";            }            else                Result = "";            Result = Result + ConvertDoubleDigit(MyNumber.Remove(0, 1));            return Result;        }        ///////////////////////////////////////// Convert Thousand Part into Word ///////////////////////////////////////////////        private string ConvertThousand(string MyNumber, int Ln)        {            string Result;            if (Ln >= 5)            {                if (ConvertDoubleDigit(MyNumber.Remove(2, 3)) == "")                    Result = "";                else                    Result = ConvertDoubleDigit(MyNumber.Remove(2,3))+" Thousand ";                Result = Result + ConvertHundred(MyNumber.Remove(0, 2));            }            else            {                if (ConvertSingleDigit(MyNumber.Remove(1, 3)) == "")                    Result = "";                else                    Result = ConvertSingleDigit(MyNumber.Remove(1, 3)) + " Thousand ";                Result = Result + ConvertHundred(MyNumber.Remove(0, 1));            }            return Result;        }        ///////////////////////////////////////// Convert Lakh Part into Word ///////////////////////////////////////////////        private string ConvertLakh(string MyNumber, int Ln)        {            string Result;            if (Ln >= 7)            {                if (ConvertDoubleDigit(MyNumber.Remove(2, 5)) == "")                    Result = "";                else                    Result = ConvertDoubleDigit(MyNumber.Remove(2, 5)) + " Lakh ";                Result = Result + ConvertThousand(MyNumber.Remove(0, 2),7);            }            else            {                if (ConvertSingleDigit(MyNumber.Remove(1, 5)) == "")                    Result = "";                else                    Result = ConvertSingleDigit(MyNumber.Remove(1, 5)) + " Lakh ";                Result = Result + ConvertThousand(MyNumber.Remove(0, 1),6);            }            return Result;        }        ///////////////////////////////////////// Convert Crore Part into Word ///////////////////////////////////////////////        private string ConvertCrore(string MyNumber, int Ln)        {            string Result;            if (Ln >= 9)            {                if (ConvertDoubleDigit(MyNumber.Remove(2, 7)) == "")                    Result = "";                else                    Result = ConvertDoubleDigit(MyNumber.Remove(2, 7)) + " Crore ";                Result = Result + ConvertLakh(MyNumber.Remove(0, 2), 9);            }            else            {                if (ConvertSingleDigit(MyNumber.Remove(1, 7)) == "")                    Result = "";                else                    Result = ConvertSingleDigit(MyNumber.Remove(1, 7)) + " Crore ";                Result = Result + ConvertLakh(MyNumber.Remove(0, 1), 8);            }            return Result;        }        /////////////////////////////////////////// Convert Double Digit into Word ////////////////////////////////////////////////////        private string ConvertDoubleDigit(string MyNumber)        {            string Result;            if (MyNumber.Remove(1, 1) == "0")                Result = ConvertSingleDigit(MyNumber.Remove(0, 1));            else            {                if (MyNumber.Remove(1, 1) == "1") // Is Decimal part between 11 & 19                {                    switch (Convert.ToInt16(MyNumber))                    {                        case 10: Result = "Ten"; break;                        case 11: Result = "Eleven"; break;                        case 12: Result = "Twelve"; break;                        case 13: Result = "Thirteen"; break;                        case 14: Result = "Fourteen"; break;                        case 15: Result = "Fifteen"; break;                        case 16: Result = "Sixteen"; break;                        case 17: Result = "Seventeen"; break;                        case 18: Result = "Eighteen"; break;                        default: Result = "Nineteen"; break;                    }                }                else //otherwise its between 20 & 99                {                    switch (Convert.ToInt16(MyNumber.Remove(1, 1)))                    {                        case 2: Result = " Twenty "; break;                        case 3: Result = " Thirty "; break;                        case 4: Result = " Fourty "; break;                        case 5: Result = " Fifty "; break;                        case 6: Result = " Sixty "; break;                        case 7: Result = " Seventy "; break;                        case 8: Result = " Eighty "; break;                        case 9: Result = " Ninety "; break;                        default: Result = " "; break;                    }                    Result = Result + ConvertSingleDigit(MyNumber.Remove(0, 1)); //Convert one's place digit                }            }            return Result;        }        /////////////////////////////////////// Convert Single Digit into Word /////////////////////////////////////////////////////        private string ConvertSingleDigit(String MyNumber)        {            string Result;            switch (Convert.ToInt16(MyNumber))            {                case 1: Result = "One"; break;                case 2: Result = "Two"; break;                case 3: Result = "Three"; break;                case 4: Result = "Four"; break;                case 5: Result = "Five"; break;                case 6: Result = "Six"; break;                case 7: Result = "Seven"; break;                case 8: Result = "Eight"; break;                case 9: Result = "Nine"; break;                default: Result = ""; break;            }            return Result;        }``