using Microsoft.VisualBasic; using System; using System.Collections; using System.Collections.Generic; using System.Diagnostics; namespace CodeMonkey { class AmountToWords { static void Main() { Console.WriteLine("Enter amount: "); string amnt = Console.ReadLine(); Console.WriteLine("{0} = {1}", amnt, NumToText(amnt)); Console.ReadKey(); } static string Ones(string strNum) { string tmp = ""; switch (strNum) { case "1": tmp = "One"; break; case "2": tmp = "Two"; break; case "3": tmp = "Three"; break; case "4": tmp = "Four"; break; case "5": tmp = "Five"; break; case "6": tmp = "Six"; break; case "7": tmp = "Seven"; break; case "8": tmp = "Eight"; break; case "9": tmp = "Nine"; break; default: tmp = ""; break; } return tmp; } static string Tens(string strNum) { string tmp = ""; string tst = Convert.ToInt32(strNum).ToString(); switch (tst) { case "10": tmp = "Ten"; break; case "11": tmp = "Eleven"; break; case "12": tmp = "Twelve"; break; case "13": tmp = "Thirteen"; break; case "14": tmp = "Fourteen"; break; case "15": tmp = "Fifteen"; break; case "16": tmp = "Sixteen"; break; case "17": tmp = "Seventeen"; break; case "18": tmp = "Eighteen"; break; case "19": tmp = "Nineteen"; break; default: tmp = Ones(Convert.ToInt32(strNum).ToString()); break; } return tmp; } static string Ties(string strNum) { string tmp = ""; string tst = Convert.ToInt32(strNum.Substring(0, 1)).ToString(); switch (tst) { case "2": tmp = "Twenty"; break; case "3": tmp = "Thirty"; break; case "4": tmp = "Forty"; break; case "5": tmp = "Fifty"; break; case "6": tmp = "Sixty"; break; case "7": tmp = "Seventy"; break; case "8": tmp = "Eighty"; break; case "9": tmp = "Ninety"; break; default: tmp = Ones(Convert.ToInt32(strNum).ToString()); break; } tmp = tmp + " " + Ones(strNum.Substring(1, 1)); return tmp; } static string Hundreds(string strNum) { string tmp = ""; if (strNum.Length == 0) tmp = ""; int ln = strNum.Length; if (ln == 3) { tmp = Ones(Convert.ToString(strNum[0])) + " Hundred "; int twos = Convert.ToInt32(strNum.Substring(1, 2)); if (twos > 19) { tmp = tmp + Ties(Convert.ToString(twos)); } else { tmp = tmp + Tens(Convert.ToString(twos)); } } else if (ln == 2) { if (Convert.ToInt32(strNum) > 19) { tmp = Ties(strNum); } else { tmp = Tens(strNum); } } else { tmp = Ones(strNum); } return tmp; } static string NumToText(string strNum) { string tmp = ""; int iLen = 0; string sCents = null; string sCentsText = ""; bool isCent = false; string _tens = null; strNum = strNum.Replace(",",""); if (strNum.Contains(".")) { string[] spl = CustomSplit(strNum); sCents = spl[1]; strNum = spl[0]; iLen = strNum.Length; isCent = true; if (Convert.ToInt32(sCents) > 19) { sCentsText = Ties(sCents); } else { sCentsText = Tens(sCents); } } else { iLen = strNum.Length; strNum = Convert.ToInt32(strNum).ToString(); } switch (iLen) { case 1: tmp = Ones(strNum); break; case 2: if (Convert.ToInt32(strNum) > 19) { _tens = Ties(Convert.ToInt32(strNum).ToString("D")); } else { _tens = Tens(Convert.ToInt32(strNum).ToString("D")); } tmp = _tens + " Pesos"; if (isCent) { tmp = tmp + " And " + sCentsText + " Cents"; } break; case 3: tmp = Hundreds(Convert.ToInt32(strNum).ToString("D")) + " Pesos"; if (isCent) { tmp = tmp + " And " + sCentsText + " Cents"; } break; case 4: tmp = Ones(Convert.ToInt32(strNum.Substring(0, 1)).ToString("D")) + " Thousand " + Hundreds(Convert.ToInt32(strNum.Substring(1, 3)).ToString("D")) + " Pesos"; if (isCent) { tmp = tmp + " And " + sCentsText + " Cents"; } break; case 5: if (Convert.ToInt32(strNum.Substring(0, 2)) > 19) { _tens = Ties(Convert.ToInt32(strNum.Substring(0, 2)).ToString("D")); } else { _tens = Tens(Convert.ToInt32(strNum.Substring(0, 2)).ToString("D")); } tmp = _tens + " Thousand " + Hundreds(Convert.ToInt32(strNum.Substring(2, 3)).ToString("D")) + " Pesos"; if (isCent) { tmp = tmp + " And " + sCentsText + " Cents"; } break; case 6: tmp = Hundreds(Convert.ToInt32(strNum.Substring(0, 3)).ToString("D")) + " Thousand " + Hundreds(Convert.ToInt32(strNum.Substring(2, 3)).ToString("D")) + " Pesos"; if (isCent) { tmp = tmp + " And " + sCentsText + " Cents"; } break; } return tmp; } static string[] CustomSplit(string str) { int pos = str.IndexOf("."); string[] tmp = new string[3]; tmp[0] = str.Substring(0, pos); tmp[1] = str.Substring(pos + 1, str.Length - (tmp[0].Length + 1)); return tmp; } } }Or you can try it online via Programmr
Tuesday, October 15, 2013
10/15/2013 11:04:00 AM
Amount to Words Converter (C#/C Sharp)
Simple code to convert a number or amount from 0 to 999,999. This was originally written in VB6(by me) but since I've been trying to learn C# lately I ported this to C# as my basic exercise. I know it's just a simple code but I'm still posting this, it might useful to someone. Here's the code:
About the Author
Unknown
I'm a modern human swiss knife. Not sure why I'm a coder I can't even rember the simplest syntax in any language lol. But I'll do anything for food so yeah I'm a programmer. And a fast one *wink*
Related Posts
Subscribe to:
Post Comments (Atom)
0 comments:
Post a Comment