Exemplo n.º 1
0
 public void Visit(LiteralNumber node)
 {
 }
Exemplo n.º 2
0
 public virtual void Visit(LiteralNumber node)
 {
 }
Exemplo n.º 3
0
        private static LiteralNumber GetWords(decimal number, bool isMoney)
        {
            if (Math.Abs(number) > 999999999999999999M)
            {
                throw new ArgumentException("Nombre trop grand. Non pris en charge :(");
            }
            LiteralNumber ret = new LiteralNumber();

            ret.OriginalNumber = number;
            if (number == 0)
            {
                ret.IntegralPart = _units[0];
                return(ret);
            }
            List <string> words = new List <string>();
            bool          neg   = false;

            if (number < 0)
            {
                neg    = true;
                number = Math.Abs(number);
            }
            decimal intPart = Math.Truncate(number);

            if (intPart == 0)
            {
                words.Add(_units[0]);
            }
            decimal       decPart     = Math.Truncate((number - intPart) * 1000);   //on se limite à 3 chiffres après la virgule
            List <string> leadingZero = new List <string>();

            if (isMoney)
            {
                decPart /= 10;                 //on ne garde que deux chiffres après la virgule (centimes)
            }
            else
            {
                if (decPart < 100)
                {
                    leadingZero.Add(_units[0]);
                }
                if (decPart < 10)
                {
                    leadingZero.Add(_units[0]);
                }
                while (decPart % 10 == 0 && decPart > 0)
                {
                    decPart /= 10;
                }
            }

            decimal temp = intPart;

            decimal[] parts = new decimal[6];             //6 parties de 3 chiffres = Million de milliards
            int       idx   = 0;

            while (Math.Truncate(temp / 1000) > 0)
            {
                parts[idx] = temp - (Math.Truncate(temp / 1000) * 1000);
                temp       = Math.Truncate(temp / 1000);
                idx++;
            }
            parts[idx] = temp;
            //parcours le tableau à l'envers
            for (int i = idx; i >= 0; i--)
            {
                if (parts[i] == 0)
                {
                    if (i == 3)
                    {
                        words.Add(_separators[i]);
                    }
                    continue;
                }
                if ((i % 3) == 1 && parts[i] == 1)
                {
                    words.Add(_separators[i]);
                    continue;
                }
                words.Add(GroupToMoneyWord(parts[i]));
                words.Add(_separators[i] + (parts[i] > 1 && (i % 4 == 2 | i % 4 == 3) ? "s" : ""));
            }
            ret.IntegralPart = string.Join(" ", words.Where(w => !string.IsNullOrWhiteSpace(w)));
            ret.IsNegative   = neg;
            if (decPart > 0)
            {
                ret.DecimalPart = (leadingZero.Count > 0 ? string.Join(" ", leadingZero) + " " : "") + GroupToMoneyWord(decPart);
            }
            return(ret);
        }