The Easiest Way to Save and Share Code Snippets on the web

NumberToPesos

abap

posted: Jun, 23rd 2009 | jump to bottom

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
 
namespace ConsoleApplication2
{
    public class Numeric
    {
        public class Spanish
        {
            public static readonly string[] Ones = { "cero", "un", "dos", "tres", "cuatro", "cinco", "seis", "siete", "ocho", "nueve", "diez", "once", "doce", "trece", "catorce", "quince", "dieciseis", "diecisiete", "dieciocho", "diecinueve", "veinte" };
            public static readonly string[] Tens = { "cero", "diez", "veinte", "treinta", "cuarenta", "cincuenta", "sesenta", "setenta", "ochenta", "noventa" };
            public static string[] Twenties = { "veinte", "veintiun", "veintidos", "veintitres", "veinticuatro", "veinticinco", "veintiseis", "veintisiete", "veintiocho", "veintinueve", "ochocientos", "novecientos" };
            public static readonly string[] Hundreds = { "cero", "ciento", "doscientos", "trescientos", "cuatrocientos", "quinientos", "seiscientos", "setecientos" };
        }
 
        protected object Value;
 
        public Numeric(int value) { this.Value = value; }
 
        public Numeric(double value) { this.Value = value; }
 
        public Numeric(float value) { this.Value = value; }
 
        public Numeric(decimal value) { this.Value = value; }
 
        public string ToSpanish() 
        {
            // -- Since we are really only handling 2x decimal places a decimal works perfectly here.
            decimal radix = decimal.Parse(this.Value.ToString());
            int v = (int)radix;
            string result = string.Empty;
 
            if (v >= 1000000 && v <= 999999999)
                result = BuildMillions(v);
            else if (v >= 1000 && v <= 999999)
                result = BuildThousands(v);
            else if (v >= 100 && v <= 999)
                result = BuildHundred(v);
            else if (v >= 0 && v <= 99)
                result = BuildTens(v);
            else
                throw new ArgumentOutOfRangeException("Number must be between 0 and 999999999 to be translated.");
 
 
            return result + " pesos " + BuildRadix(radix);
        }
 
        private string BuildTens(int v) 
        {
            if (v >= 0 && v <= 20)
                return Spanish.Ones[v];
            else if (v > 20 && v < 30)
                return string.Format("{0}", Spanish.Twenties[v - 20]);
            else if (v > 20 && v < 100)
            {
                int index1 = v / 10;
                int index2 = v - (index1 * 10);
                return index2 == 0 ?
                    Spanish.Tens[index1] :
                    string.Format("{0} y {1}", Spanish.Tens[index1], Spanish.Ones[index2]);
            }
            else throw new ArgumentOutOfRangeException("Number must be between 0 and 99");
        }
 
        private string BuildHundred(int v) 
        {
            if (v >= 100 && v <= 999)
            {
                int hundred = v / 100;
                int remainder = v - (hundred * 100);
                return string.Format("{0} {1}", Spanish.Hundreds[hundred], BuildTens(remainder));
            }
            else throw new ArgumentOutOfRangeException("Number must be between 100 and 999");
        }
 
        private string BuildThousands(int v) 
        {
            if (v >= 1000 && v <= 999999)
            {
                int thousand = v / 1000;
                int remainder = v - (thousand * 1000);
                return string.Format("{0} mil {1}", BuildHundred(thousand), BuildHundred(remainder));
            }
            else throw new ArgumentOutOfRangeException("Number must be between 1000 and 999999");
        }
 
        private string BuildMillions(int v) 
        {
            if (v >= 1000000 && v <= 999999999)
            {
                int million = v / 1000000;
                int remainder = v - (million * 1000000);
                return string.Format("{0} millones {1}", BuildHundred(million), BuildThousands(remainder));
            }
            else throw new ArgumentOutOfRangeException("Number must be between 1000000 and 999999999");
        }
 
        private string BuildRadix(decimal v) 
        {
            string radixString = "y {0}{1}{2} centavos";
 
            if (v.HasRadix())
            {
                // -- Change is centavos        
                int radix = v.GetRadix();
                if (radix > 0 && radix < 100)
                {
                    return string.Format("y {0} centavos", BuildTens(radix));
                }
                else throw new Exception("Invalid radix.");
            }
            else
            {
                radixString = string.Format(radixString, Spanish.Ones[0], string.Empty, string.Empty);
            }
 
            return radixString;
        }
    }
 
    public static class NumericExtensions
    {
        public static bool HasRadix<T>(this T v)
            where T : struct, IComparable, IComparable<T>, IEquatable<T> 
        {
            return v.ToString().Contains(".");
        }
 
        public static int GetRadix<T>(this T v)
            where T : struct, IComparable, IComparable<T>, IEquatable<T>
        {
            if (v.HasRadix())
            {
                decimal temp = decimal.Parse(v.ToString().Substring(v.ToString().IndexOf(".")));
                return (int)(temp * 100);
            }
            else return 0; // -- Nothing really to print.
        }
    }
 
    class Program
    {
        static void Main(string[] args)
        {
            var date = DateTime.Now;
            Console.WriteLine(new Numeric(633623202.16).ToSpanish());
            Console.WriteLine(DateTime.Now - date);            
        }
    }
}
 
249 views