示例#1
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);
        }
	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;
	}