コード例 #1
0
ファイル: GeneralFormatter.cs プロジェクト: ForNeVeR/pnet
        public override string Format(Object o, IFormatProvider provider)
        {
            int precision, exponent;

            if (this.precision == -1)
            {
                precision = PrecisionOf(o);
            }
            else
            {
                precision = this.precision;
            }

#if CONFIG_EXTENDED_NUMERICS
            double val = OToDouble(o);
            if (val == 0.0)
            {
                exponent = 0;
            }
            else
            {
                //exponent = (int) Math.Floor(Math.Log10(Math.Abs(OToDouble(o))));
                exponent = Formatter.GetExponent(val);
            }
#else
            // Determine the exponent without using floating-point.
            if (IsSignedInt(o))
            {
                long lvalue = OToLong(o);
                if (lvalue < 0)
                {
                    lvalue = -lvalue;
                }
                exponent = 0;
                while (lvalue >= 10)
                {
                    ++exponent;
                    lvalue /= 10;
                }
            }
            else if (IsUnsignedInt(o))
            {
                ulong ulvalue = OToUlong(o);
                exponent = 0;
                while (ulvalue >= 10)
                {
                    ++exponent;
                    ulvalue /= 10;
                }
            }
            else
            {
                exponent = 0;
            }
#endif

            if (IsSignedInt(o) || IsUnsignedInt(o))
            {
                if (exponent < precision)
                {
                    return(StripTrail(
                               FixedPointFormatter.Format(o, 0, provider),
                               provider));
                }
                else
                {
                    return
                        (new ScientificFormatter(-1, Ee).Format(o, provider));
                }
            }

            if (exponent >= -4 && exponent < precision)
            {
#if CONFIG_EXTENDED_NUMERICS
                if (!IsDecimal(o))
                {
                    return(Formatter.FormatDouble(val, exponent, precision, false, provider));
                }
#endif
                return(StripTrail(
                           FixedPointFormatter.Format(o, precision, provider),
                           provider));
            }
            else
            {
#if CONFIG_EXTENDED_NUMERICS
                return(Formatter.FormatScientific(val, exponent, precision, this.Gg.ToString(), provider));
#else
                return(new ScientificFormatter(precision, Ee).Format(o, provider));
#endif
            }
        }
コード例 #2
0
        public static Formatter CreateFormatter(String format, IFormatProvider p)
        {
            int       precision;
            Formatter ret;

            if (format == null)
            {
                throw new FormatException(_("Format_StringException"));
            }

            // handle empty format
            if (format.Length == 0)
            {
                ret             = new GeneralFormatter(-1, 'G');
                formats[format] = ret;
                return(ret);
            }

            //  Search for cached formats
            if (formats[format] != null)
            {
                return((Formatter)formats[format]);
            }


            // Validate the format.
            // It should be of the form 'X', 'X9', or 'X99'.
            // If it's not, return a CustomFormatter.
            if (validformats.IndexOf(format[0]) == -1 || format.Length > 3)
            {
                ret             = new CustomFormatter(format);
                formats[format] = ret;
                return(ret);
            }

            try
            {
                precision = (format.Length == 1) ?
                            -1 : Byte.Parse(format.Substring(1));
            }
            catch (FormatException)
            {
                ret             = new CustomFormatter(format);
                formats[format] = ret;
                return(ret);
            }

            switch (format[0])          // There's always a yucky switch somewhere
            {
            case 'C':
            case 'c':
                ret = new CurrencyFormatter(precision);
                break;

            case 'D':
            case 'd':
                ret = new DecimalFormatter(precision);
                break;

            case 'E':
            case 'e':
                ret = new ScientificFormatter(precision, format[0]);
                break;

            case 'F':
            case 'f':
                ret = new FixedPointFormatter(precision);
                break;

            case 'G':
            case 'g':
                ret = new GeneralFormatter(precision, format[0]);
                break;

            case 'N':
            case 'n':
                ret = new System.Private.NumberFormat.NumberFormatter(precision);
                break;

            case 'P':
            case 'p':
                ret = new PercentFormatter(precision);
                break;

            case 'R':
            case 'r':
                ret = new RoundTripFormatter(precision);
                break;

            case 'X':
            case 'x':
                ret = new HexadecimalFormatter(precision, format[0]);
                break;

            default:
                ret = new CustomFormatter(format);
                break;
            }

            formats[format] = ret;
            return(ret);
        }
コード例 #3
0
	public static Formatter CreateFormatter(String format, IFormatProvider p)
	{
		int precision;
		Formatter ret;

		if (format == null)
		{
			throw new FormatException(_("Format_StringException"));
		}

		// handle empty format
		if(format.Length == 0)
		{
			//return new CustomFormatter(format);
			return new GeneralFormatter(-1, 'G');
		}

		//  Search for cached formats
		if (formats[format] != null)
		{
			return (Formatter) formats[format];
		}

		// Validate the format.  
		// It should be of the form 'X', 'X9', or 'X99'.
		// If it's not, return a CustomFormatter.
		if (validformats.IndexOf(format[0]) == -1 || format.Length > 3)
		{
			return new CustomFormatter(format);
		}

		try 
		{
			precision = (format.Length == 1) ? 
				-1 : Byte.Parse(format.Substring(1));
		}
		catch (FormatException)
		{
			return new CustomFormatter(format);
		}
		
		switch(format[0])	// There's always a yucky switch somewhere
		{
		case 'C':
		case 'c':
			ret = new CurrencyFormatter(precision);
			break;
		
		case 'D':
		case 'd':
			ret = new DecimalFormatter(precision);
			break;
		
		case 'E':
		case 'e':
			ret = new ScientificFormatter(precision, format[0]);
			break;
		
		case 'F':
		case 'f':
			ret = new FixedPointFormatter(precision);
			break;

		case 'G':
		case 'g':
			ret = new GeneralFormatter(precision, format[0]);
			break;

		case 'N':
		case 'n':
			ret = new System.Private.NumberFormat.NumberFormatter(precision);
			break;

		case 'P':
		case 'p':
			ret = new PercentFormatter(precision);
			break;
		
		case 'R':
		case 'r':
			ret = new RoundTripFormatter(precision);
			break;

		case 'X':
		case 'x':
			ret = new HexadecimalFormatter(precision, format[0]);
			break;

		default:
			ret = new CustomFormatter(format);
			break;
		}
		
		formats[format] = ret;
		return ret;
	}