Tuesday, February 26, 2013

C# Numbers to Words example


        public static string NumberToWords(this Decimal d)
        {
            if (d == 0)
            {
                return ("Zero Dollars and No Cents");
            }
            d = Math.Abs(d);
            string[] ones = { "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine" };
            string[] teens = { "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen" };
            string[] tens = { "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety" };
            string[] magnitude = { "Hundred", "Thousand", "Million", "Billion", "Trillion", "Quadrillion", "WholeBunch" };
            byte Digit1, Digit2, Digit3, Digit2And3;
            int ndx;
            ArrayList wordSegments = new ArrayList();
            string wordAmount = string.Empty;
            string wordSegment = string.Empty;
            Int64 dollarAmount = Convert.ToInt64(Math.Floor(d));
            byte centsAmount = Convert.ToByte((System.Convert.ToDouble(d) - System.Convert.ToDouble(dollarAmount)) * 100);
            string segmentedNumber = d.ToString("###,###.00");
            string[] NumericSegments = segmentedNumber.Split(new string[] { ",", "." }, StringSplitOptions.RemoveEmptyEntries);
            for (ndx = 0; ndx < NumericSegments.Length; ndx++)
            {
                NumericSegments[ndx] = NumericSegments[ndx].PadLeft(3, Convert.ToChar("0"));
            }
            foreach (string NumericSegment in NumericSegments)
            {
                Digit1 = Convert.ToByte(Convert.ToString(NumericSegment[0]));
                Digit2 = Convert.ToByte(Convert.ToString(NumericSegment[1]));
                Digit3 = Convert.ToByte(Convert.ToString(NumericSegment[2]));
                Digit2And3 = Convert.ToByte(NumericSegment.Substring(1, 2));

                if (Digit1 > 0)  //in the hundreds
                {
                    wordSegment = ones[Digit1 - 1] + " Hundred";
                }
                if (Digit2And3 > 19) //over the teens
                {
                    wordSegment += (wordSegment.Length > 0 ? " and " : " ") + tens[Digit2 - 2];

                    if (Digit3 > 0) //has a lower decimal
                    {
                        wordSegment += "-" + ones[Digit3 - 1];
                    }
                }
                else
                {
                    if (Digit2And3 > 9) //teens
                    {
                        wordSegment += (wordSegment.Length > 0 ? " and " : " ") + teens[Digit3];
                    }
                    else
                    {
                        if (Digit3 > 0) //under teens
                        {
                            wordSegment += " " + ones[Digit3 - 1];
                        }
                    }
                }

                wordSegments.Add(wordSegment);
                wordSegment = string.Empty;
            }

            wordSegments.Reverse();

            for (ndx = 0; ndx < wordSegments.Count; ndx++)
            {
                string currentSegment = (string)wordSegments[ndx];
                switch (ndx)
                {
                    case 0:
                    wordAmount = (centsAmount == 0 ? " No Cents" : currentSegment + " Cent" + (centsAmount > 1 ? "s" : ""));
                    break;
                    case 1:
                    wordAmount = currentSegment + " Dollar" + (dollarAmount > 1 ? "s" : "") + " and" + wordAmount;
                    break;
                    default:
                    wordAmount = currentSegment + " " + magnitude[ndx - 1] + ", " + wordAmount;
                    break;
                }
            }
            wordAmount = wordAmount.Trim();
            string savedWordAmount = wordAmount;


            //wraps the string once
            try
            {
                const int maxLength = 70;
                if (wordAmount.Length > maxLength)
                {
                    string firstbit = wordAmount.Substring(0, maxLength);
                    int lastSpace = firstbit.LastIndexOf(" ");
                    wordAmount = wordAmount.Remove(lastSpace, 1);
                    wordAmount = wordAmount.Insert(lastSpace, Environment.NewLine);
                }

            }
            catch (Exception ex)
            {
                wordAmount = savedWordAmount;
            }

            return wordAmount;
        }


No comments:

Post a Comment