Example #1
0
        /// <summary>
        /// Appends a word representation of the provided base to a StringBuilder.
        /// For example:
        /// > 123 456 789 --thousand--> 456 ---> forty-five hundred six thousand
        /// </summary>
        /// <param name="numeral">The base numeral.</param>
        /// <param name="num">The number of which the base gets appended to.</param>
        /// <param name="str">StringBuilder the word representation gets appended to.</param>
        /// <returns>True, if something was appended.</returns>
        public static bool AppendTo(Numerals.Large numeral, uint num, StringBuilder str)
        {
            uint baseVal = numeral.AsNumber();

            string baseWord = numeral.ToString();

            // get the denotator of the base out of the number,
            // e.g.
            //
            // 123 456 789  --thousands--> 456
            uint part = (num / baseVal) % baseVal;

            // if there is no denotator, skip:
            if (part == 0)
            {
                return(false);
            }

            // otherwise: put this into words.
            HundredSegment.AppendTo(part, str);
            str.Append(" ");
            str.Append(baseWord);

            return(true);
        }
        /// <summary>
        /// Converts an amount of dollars and cents into its word representation.
        /// Zero cents are ignored, the upper border MaxDollars applies for the
        /// amount of dollars, as well as 99 as an upper border for cents.
        /// </summary>
        /// <param name="dollars">Amount of dollars.</param>
        /// <param name="cents">Amount of cents.</param>
        /// <returns>Word representation of dollars and cents.</returns>
        public static string ToString(uint dollars, uint cents)
        {
            if (dollars > MaxDollars)
            {
                throw new ArgumentOutOfRangeException(null, String.Format("Dollars must be within {0}", MaxDollars));
            }
            if (cents > 99)
            {
                throw new ArgumentOutOfRangeException(null, String.Format("Cents must be within {0}", 99));
            }


            //--------------------
            // STRING BUILDING
            //--------------------
            // String is built in an appending manner, so, given any number
            // 123 456 789 , 01
            // This goes:
            // - Append Millions
            // - Append Thousands
            // - Append Hundred
            // - Append Dollar Suffix
            // - Append Cents
            // - Append Cents Suffix
            //
            // With a special case for zero dollars.

            StringBuilder result = new StringBuilder(_StorageSize);

            // special case switch:
            if (dollars == 0)
            {
                HundredSegment.AppendToWithZero(dollars, result);
                result.Append(" dollars");
            }
            else
            {
                if (ThousandBaseSegment.AppendTo(Numerals.Large.million, dollars, result))
                {
                    result.Append(" ");
                }
                if (ThousandBaseSegment.AppendTo(Numerals.Large.thousand, dollars, result))
                {
                    result.Append(" ");
                }
                if (HundredSegment.AppendTo(dollars % 1000, result))
                {
                    result.Append(" ");
                }

                // dollar suffix:
                if (dollars == 1)
                {
                    result.Append("dollar");
                }
                else
                {
                    result.Append("dollars");
                }
            }

            // add cents, if nec.:
            if (cents > 0)
            {
                result.Append(" and ");                 // even for 0 dollars, this would be "zero dollars", so an " and " is needed always.
                HundredSegment.AppendTo(cents, result); // add cents word rep.

                // cent suffix:
                if (cents == 1)
                {
                    result.Append(" cent");
                }
                else
                {
                    result.Append(" cents");
                }
            }

            return(result.ToString());
        }