1 /* Copyright (C) 1998, 1999 Cygnus Solutions
3 This file is part of libgcj.
5 This software is copyrighted work licensed under the terms of the
6 Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
11 import java.util.Locale;
12 import java.util.ResourceBundle;
13 import java.util.MissingResourceException;
16 * @author Tom Tromey <tromey@cygnus.com>
19 /* Written using "Java Class Libraries", 2nd edition, plus online
20 * API docs for JDK 1.2 from http://www.javasoft.com.
21 * Status: Believed complete and correct to 1.2, except serialization
22 * and getAvailableLocales.
25 public abstract class NumberFormat extends Format implements Cloneable
27 public static final int INTEGER_FIELD = 0;
28 public static final int FRACTION_FIELD = 1;
30 public final String format (long number)
32 StringBuffer sbuf = new StringBuffer(50);
33 format (number, sbuf, null);
34 return sbuf.toString();
37 public final StringBuffer format (Object obj, StringBuffer sbuf,
40 return format(((Number) obj).doubleValue(), sbuf, pos);
43 public abstract StringBuffer format (double number,
44 StringBuffer sbuf, FieldPosition pos);
46 public abstract StringBuffer format (long number,
47 StringBuffer sbuf, FieldPosition pos);
49 public static Locale[] getAvailableLocales ()
55 private static final NumberFormat computeInstance (Locale loc,
62 res = ResourceBundle.getBundle("gnu.gcj.text.LocaleData", loc);
64 catch (MissingResourceException x)
71 fmt = res == null ? def : res.getString(resource);
73 catch (MissingResourceException x)
77 DecimalFormatSymbols dfs = new DecimalFormatSymbols (loc);
78 return new DecimalFormat (fmt, dfs);
81 public static final NumberFormat getCurrencyInstance ()
83 return getCurrencyInstance (Locale.getDefault());
86 public static NumberFormat getCurrencyInstance (Locale loc)
88 return computeInstance (loc, "currencyFormat", "$#,##0.00;($#,##0.00)");
91 public static final NumberFormat getInstance ()
93 return getInstance (Locale.getDefault());
96 public static NumberFormat getInstance (Locale loc)
98 // For now always return a number instance.
99 return getNumberInstance (loc);
102 public int getMaximumFractionDigits ()
104 return maximumFractionDigits;
107 public int getMaximumIntegerDigits ()
109 return maximumIntegerDigits;
112 public int getMinimumFractionDigits ()
114 return minimumFractionDigits;
117 public int getMinimumIntegerDigits ()
119 return minimumIntegerDigits;
122 public static final NumberFormat getNumberInstance ()
124 return getNumberInstance (Locale.getDefault());
127 public static NumberFormat getNumberInstance (Locale loc)
129 return computeInstance (loc, "numberFormat", "#,##0.###");
132 public static final NumberFormat getPercentInstance ()
134 return getPercentInstance (Locale.getDefault());
137 public static NumberFormat getPercentInstance (Locale loc)
139 return computeInstance (loc, "percentFormat", "#,##0%");
142 public int hashCode ()
144 int hash = super.hashCode();
145 hash ^= (maximumFractionDigits + maximumIntegerDigits
146 + minimumFractionDigits + minimumIntegerDigits);
149 if (parseIntegerOnly)
154 public boolean isGroupingUsed ()
159 public boolean isParseIntegerOnly ()
161 return parseIntegerOnly;
164 public NumberFormat ()
168 public abstract Number parse (String sourceStr, ParsePosition pos);
170 public Number parse (String sourceStr) throws ParseException
172 ParsePosition pp = new ParsePosition (0);
173 Number r = parse (sourceStr, pp);
176 int index = pp.getErrorIndex();
178 index = pp.getIndex();
179 throw new ParseException ("couldn't parse number", index);
184 public final Object parseObject (String sourceStr, ParsePosition pos)
186 return parse (sourceStr, pos);
189 public void setGroupingUsed (boolean newValue)
191 groupingUsed = newValue;
194 public void setMaximumFractionDigits (int newValue)
196 maximumFractionDigits = newValue;
199 public void setMaximumIntegerDigits (int newValue)
201 maximumIntegerDigits = newValue;
204 public void setMinimumFractionDigits (int newValue)
206 minimumFractionDigits = newValue;
209 public void setMinimumIntegerDigits (int newValue)
211 minimumIntegerDigits = newValue;
214 public void setParseIntegerOnly (boolean value)
216 parseIntegerOnly = value;
219 public final String format (double number)
221 StringBuffer sbuf = new StringBuffer(50);
222 format (number, sbuf, null);
223 return sbuf.toString();
226 // These field names are fixed by the serialization spec.
227 // FIXME: serialization spec also mentions `byte' versions of the
228 // min/max fields. We have no use for those, so for now they are
230 protected boolean groupingUsed;
231 protected int maximumFractionDigits;
232 protected int maximumIntegerDigits;
233 protected int minimumFractionDigits;
234 protected int minimumIntegerDigits;
235 protected boolean parseIntegerOnly;