Ejemplo n.º 1
0
        public static string NumberToString(string format, NumberFormatInfo nfi, Decimal value)
        {
            String hex;
            char   specifier;
            int    precision, digits, decimals;
            bool   underflow;

            //Console.WriteLine("!DecimalFormatter ToString format="+format);

            format = format.Trim();
            if (!DecimalFormatter.ParseFormat(format, out specifier, out precision))
            {
                throw new FormatException(Locale.GetText("The specified format is invalid"));
            }

            if (specifier == 'X')
            {
                digits    = (precision >= 0) ? precision : 0;
                underflow = DecimalEx.decimal2HexString(value, digits, format[0] == 'x', out hex);
                if (underflow)
                {
                    throw new FormatException(Locale.GetText("The specified format is invalid"));
                }

                return(hex);
            }

            digits   = -1;
            decimals = 0;
            // first calculate number of digits or decimals needed for format
            switch (specifier)
            {
            case 'C':
                decimals = (precision >= 0) ? precision : nfi.CurrencyDecimalDigits;
                break;

            case 'F': goto case 'N';

            case 'N':
                decimals = (precision >= 0) ? precision : nfi.NumberDecimalDigits;
                break;

            case 'G':
                digits = (precision >= 0) ? precision : 0;
                break;

            case 'E':
                digits = (precision >= 0) ? precision + 1 : 7;
                break;

            case 'P':
                decimals = (precision >= 0) ? precision + 2 : nfi.PercentDecimalDigits + 2;
                break;

            case 'Z':
                digits = 0;
                break;
            }

            // get digit string
            const int bufSize = 40;
            int       decPos = 0, sign = 0;

            char[] buf = new char[bufSize];
//			Console.WriteLine("  !calling decimal2string with lo {1}, mid {2}, hi {3} ,ss={0}",value.ss32,value.lo32,value.mid32,value.hi32);
            DecimalEx.decimal2string(value, digits, decimals, buf, bufSize, out decPos, out sign);
            string TempString = new String(buf);

            TempString = TempString.Trim(new char[] { (char)0x0 });

            //Console.WriteLine("  decimal2string returned '{0}'", TempString);
            StringBuilder sb = new StringBuilder(TempString, TempString.Length);

            if (sb.ToString() == String.Empty && decPos > 0 && sign == 0)
            {
                sb.Append('0');
            }

//			Console.WriteLine("  str '{0}', digits {1}, decPos {2}, sign {3} specifier={4}", TempString, digits, decPos, sign,specifier);

            // now build the format
            switch (specifier)
            {
            case 'C': return(FormatCurrency(nfi, sb, decimals, decPos, sign));

            case 'N': return(FormatNumber(nfi, sb, decimals, decPos, sign));

            case 'F': return(FormatFixedPoint(nfi, sb, decimals, decPos, sign));

            case 'G': return(FormatGeneral(nfi, sb, digits, decPos, sign, format[0]));

            case 'E': return(FormatExponential(nfi, sb, digits, decPos, sign, format[0], true));

            case 'P': return(FormatPercent(nfi, sb, decimals, decPos, sign));

            case 'Z': return(FormatNormalized(nfi, sb, digits, decPos, sign));

            default:
                throw new FormatException(Locale.GetText("The specified format is invalid"));
            }
        }