]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-4_2_1-src/src/com/ibm/icu/dev/eclipse/plugins/com.ibm.icu.base/src/com/ibm/icu/text/NumberFormat.java
icu4jsrc
[Dictionary.git] / jars / icu4j-4_2_1-src / src / com / ibm / icu / dev / eclipse / plugins / com.ibm.icu.base / src / com / ibm / icu / text / NumberFormat.java
1 /*\r
2  *******************************************************************************\r
3  * Copyright (C) 1996-2008, International Business Machines Corporation and    *\r
4  * others. All Rights Reserved.                                                *\r
5  *******************************************************************************\r
6  */\r
7 \r
8 \r
9 package com.ibm.icu.text;\r
10 \r
11 import java.lang.reflect.InvocationTargetException;\r
12 import java.lang.reflect.Method;\r
13 import java.math.BigInteger;\r
14 import java.text.DecimalFormatSymbols;\r
15 import java.text.FieldPosition;\r
16 import java.text.Format;\r
17 import java.text.ParseException;\r
18 import java.text.ParsePosition;\r
19 import java.util.Locale;\r
20 \r
21 import com.ibm.icu.util.ULocale;\r
22 \r
23 /**\r
24  * <code>NumberFormat</code> is the abstract base class for all number\r
25  * formats. This class provides the interface for formatting and parsing\r
26  * numbers. <code>NumberFormat</code> also provides methods for determining\r
27  * which locales have number formats, and what their names are.\r
28  *\r
29  * <p><strong>This is an enhanced version of <code>NumberFormat</code> that\r
30  * is based on the standard version in the JDK.  New or changed functionality\r
31  * is labeled\r
32  * <strong><font face=helvetica color=red>NEW</font></strong> or\r
33  * <strong><font face=helvetica color=red>CHANGED</font></strong>.</strong>\r
34  *\r
35  * <p>\r
36  * <code>NumberFormat</code> helps you to format and parse numbers for any locale.\r
37  * Your code can be completely independent of the locale conventions for\r
38  * decimal points, thousands-separators, or even the particular decimal\r
39  * digits used, or whether the number format is even decimal.\r
40  *\r
41  * <p>\r
42  * To format a number for the current Locale, use one of the factory\r
43  * class methods:\r
44  * <blockquote>\r
45  * <pre>\r
46  *  myString = NumberFormat.getInstance().format(myNumber);\r
47  * </pre>\r
48  * </blockquote>\r
49  * If you are formatting multiple numbers, it is\r
50  * more efficient to get the format and use it multiple times so that\r
51  * the system doesn't have to fetch the information about the local\r
52  * language and country conventions multiple times.\r
53  * <blockquote>\r
54  * <pre>\r
55  * NumberFormat nf = NumberFormat.getInstance();\r
56  * for (int i = 0; i < a.length; ++i) {\r
57  *     output.println(nf.format(myNumber[i]) + "; ");\r
58  * }\r
59  * </pre>\r
60  * </blockquote>\r
61  * To format a number for a different Locale, specify it in the\r
62  * call to <code>getInstance</code>.\r
63  * <blockquote>\r
64  * <pre>\r
65  * NumberFormat nf = NumberFormat.getInstance(Locale.FRENCH);\r
66  * </pre>\r
67  * </blockquote>\r
68  * You can also use a <code>NumberFormat</code> to parse numbers:\r
69  * <blockquote>\r
70  * <pre>\r
71  * myNumber = nf.parse(myString);\r
72  * </pre>\r
73  * </blockquote>\r
74  * Use <code>getInstance</code> or <code>getNumberInstance</code> to get the\r
75  * normal number format. Use <code>getIntegerInstance</code> to get an\r
76  * integer number format. Use <code>getCurrencyInstance</code> to get the\r
77  * currency number format. And use <code>getPercentInstance</code> to get a\r
78  * format for displaying percentages. With this format, a fraction like\r
79  * 0.53 is displayed as 53%.\r
80  *\r
81  * <p>\r
82  * You can also control the display of numbers with such methods as\r
83  * <code>setMinimumFractionDigits</code>.\r
84  * If you want even more control over the format or parsing,\r
85  * or want to give your users more control,\r
86  * you can try casting the <code>NumberFormat</code> you get from the factory methods\r
87  * to a <code>DecimalFormat</code>. This will work for the vast majority\r
88  * of locales; just remember to put it in a <code>try</code> block in case you\r
89  * encounter an unusual one.\r
90  *\r
91  * <p>\r
92  * NumberFormat is designed such that some controls\r
93  * work for formatting and others work for parsing.  The following is\r
94  * the detailed description for each these control methods,\r
95  * <p>\r
96  * setParseIntegerOnly : only affects parsing, e.g.\r
97  * if true,  "3456.78" -> 3456 (and leaves the parse position just after '6')\r
98  * if false, "3456.78" -> 3456.78 (and leaves the parse position just after '8')\r
99  * This is independent of formatting.  If you want to not show a decimal point\r
100  * where there might be no digits after the decimal point, use\r
101  * setDecimalSeparatorAlwaysShown on DecimalFormat.\r
102  * <p>\r
103  * You can also use forms of the <code>parse</code> and <code>format</code>\r
104  * methods with <code>ParsePosition</code> and <code>FieldPosition</code> to\r
105  * allow you to:\r
106  * <ul>\r
107  * <li> progressively parse through pieces of a string\r
108  * <li> align the decimal point and other areas\r
109  * </ul>\r
110  * For example, you can align numbers in two ways:\r
111  * <ol>\r
112  * <li> If you are using a monospaced font with spacing for alignment,\r
113  *      you can pass the <code>FieldPosition</code> in your format call, with\r
114  *      <code>field</code> = <code>INTEGER_FIELD</code>. On output,\r
115  *      <code>getEndIndex</code> will be set to the offset between the\r
116  *      last character of the integer and the decimal. Add\r
117  *      (desiredSpaceCount - getEndIndex) spaces at the front of the string.\r
118  *\r
119  * <li> If you are using proportional fonts,\r
120  *      instead of padding with spaces, measure the width\r
121  *      of the string in pixels from the start to <code>getEndIndex</code>.\r
122  *      Then move the pen by\r
123  *      (desiredPixelWidth - widthToAlignmentPoint) before drawing the text.\r
124  *      It also works where there is no decimal, but possibly additional\r
125  *      characters at the end, e.g., with parentheses in negative\r
126  *      numbers: "(12)" for -12.\r
127  * </ol>\r
128  *\r
129  * <h4>Synchronization</h4>\r
130  * <p>\r
131  * Number formats are generally not synchronized. It is recommended to create \r
132  * separate format instances for each thread. If multiple threads access a format\r
133  * concurrently, it must be synchronized externally. \r
134  * <p>\r
135  *\r
136  * <h4>DecimalFormat</h4>\r
137  * <p>DecimalFormat is the concrete implementation of NumberFormat, and the\r
138  * NumberFormat API is essentially an abstraction from DecimalFormat's API.\r
139  * Refer to DecimalFormat for more information about this API.</p>\r
140  *\r
141  * see          DecimalFormat\r
142  * see          java.text.ChoiceFormat\r
143  * @author       Mark Davis\r
144  * @author       Helena Shih\r
145  * @author       Alan Liu\r
146  * @stable ICU 2.0\r
147  */\r
148 public class NumberFormat extends Format {\r
149     private static final long serialVersionUID = 1;\r
150 \r
151     /**\r
152      * @internal\r
153      */\r
154     public final java.text.NumberFormat numberFormat;\r
155         \r
156     /**\r
157      * @internal\r
158      * @param delegate the NumberFormat to which to delegate\r
159      */\r
160     public NumberFormat(java.text.NumberFormat delegate) {\r
161         this.numberFormat = delegate;\r
162     }\r
163     \r
164     /**\r
165      * Default constructor to mirror Java's default public\r
166      * constructor.  Java's is not callable as a public API, since\r
167      * their NumberFormat is abstract, so this is only useful to\r
168      * subclasses.  In general, subclasses will not work unless\r
169      * they manipulate the delegate.\r
170      */\r
171     public NumberFormat() {\r
172         this.numberFormat = java.text.NumberFormat.getInstance();\r
173     }\r
174 \r
175     /**\r
176      * Field constant used to construct a FieldPosition object. Signifies that\r
177      * the position of the integer part of a formatted number should be returned.\r
178      * @see java.text.FieldPosition\r
179      * @stable ICU 2.0\r
180      */\r
181     public static final int INTEGER_FIELD = 0;\r
182         \r
183     /**\r
184      * Field constant used to construct a FieldPosition object. Signifies that\r
185      * the position of the fraction part of a formatted number should be returned.\r
186      * @see java.text.FieldPosition\r
187      * @stable ICU 2.0\r
188      */\r
189     public static final int FRACTION_FIELD = 1;\r
190         \r
191     /**\r
192      * <strong><font face=helvetica color=red>CHANGED</font></strong>\r
193      * Format an object.  Change: recognizes <code>BigInteger</code>\r
194      * and <code>BigDecimal</code> objects.\r
195      * @stable ICU 2.0\r
196      */\r
197     public StringBuffer format(Object number, StringBuffer toAppendTo, FieldPosition pos) {\r
198         return numberFormat.format(number, toAppendTo, pos);\r
199     }\r
200         \r
201     /**\r
202      * @stable ICU 2.0\r
203      */\r
204     public final Object parseObject(String source, ParsePosition parsePosition) {\r
205         return numberFormat.parse(source, parsePosition);\r
206     }\r
207         \r
208     /**\r
209      * Specialization of format.\r
210      * @see java.text.Format#format(Object)\r
211      * @stable ICU 2.0\r
212      */\r
213     public final String format(double number) {\r
214         return numberFormat.format(number);\r
215     }\r
216         \r
217     /**\r
218      * Specialization of format.\r
219      * @see java.text.Format#format(Object)\r
220      * @stable ICU 2.0\r
221      */\r
222     public final String format(long number) {\r
223         return numberFormat.format(number);\r
224     }\r
225         \r
226     /**\r
227      * <strong><font face=helvetica color=red>NEW</font></strong>\r
228      * Convenience method to format a BigInteger.\r
229      * @stable ICU 2.0\r
230      */\r
231     public final String format(BigInteger number) {\r
232         return numberFormat.format(number);\r
233     }\r
234         \r
235     /**\r
236      * Specialization of format.\r
237      * @see java.text.Format#format(Object, StringBuffer, FieldPosition)\r
238      * @stable ICU 2.0\r
239      */\r
240     public StringBuffer format(double number, StringBuffer toAppendTo, FieldPosition pos) {\r
241         return numberFormat.format(number, toAppendTo, pos);\r
242     }\r
243         \r
244     /**\r
245      * Specialization of format.\r
246      * @see java.text.Format#format(Object, StringBuffer, FieldPosition)\r
247      * @stable ICU 2.0\r
248      */\r
249     public StringBuffer format(long number, StringBuffer toAppendTo, FieldPosition pos) {\r
250         return numberFormat.format(number, toAppendTo, pos);\r
251     }\r
252         \r
253     /**\r
254      * <strong><font face=helvetica color=red>NEW</font></strong>\r
255      * Format a BigInteger.\r
256      * @see java.text.Format#format(Object, StringBuffer, FieldPosition)\r
257      * @stable ICU 2.0\r
258      */\r
259     public StringBuffer format(BigInteger number, StringBuffer toAppendTo, FieldPosition pos) {\r
260         return numberFormat.format(number, toAppendTo, pos);\r
261     }\r
262     /**\r
263      * Returns a Long if possible (e.g., within the range [Long.MIN_VALUE,\r
264      * Long.MAX_VALUE] and with no decimals), otherwise a Double.\r
265      * If IntegerOnly is set, will stop at a decimal\r
266      * point (or equivalent; e.g., for rational numbers "1 2/3", will stop\r
267      * after the 1).\r
268      * Does not throw an exception; if no object can be parsed, index is\r
269      * unchanged!\r
270      * @see #isParseIntegerOnly\r
271      * @see java.text.Format#parseObject(String, ParsePosition)\r
272      * @stable ICU 2.0\r
273      */\r
274     public Number parse(String text, ParsePosition parsePosition) {\r
275         return numberFormat.parse(text, parsePosition);\r
276     }\r
277         \r
278     /**\r
279      * Parses text from the beginning of the given string to produce a number.\r
280      * The method might not use the entire text of the given string.\r
281      *\r
282      * @param text A String whose beginning should be parsed.\r
283      * @return A Number parsed from the string.\r
284      * @exception ParseException if the beginning of the specified string \r
285      * cannot be parsed.\r
286      * @see #format\r
287      * @stable ICU 2.0\r
288      */\r
289     public Number parse(String text) throws ParseException {\r
290         return numberFormat.parse(text);\r
291     }\r
292     /**\r
293      * Returns true if this format will parse numbers as integers only.\r
294      * For example in the English locale, with ParseIntegerOnly true, the\r
295      * string "1234." would be parsed as the integer value 1234 and parsing\r
296      * would stop at the "." character.  The decimal separator accepted\r
297      * by the parse operation is locale-dependent and determined by the\r
298      * subclass.\r
299      * @return true if this will parse integers only\r
300      * @stable ICU 2.0\r
301      */\r
302     public boolean isParseIntegerOnly() {\r
303         return numberFormat.isParseIntegerOnly();\r
304     }\r
305         \r
306     /**\r
307      * Sets whether or not numbers should be parsed as integers only.\r
308      * @param value true if this should parse integers only\r
309      * @see #isParseIntegerOnly\r
310      * @stable ICU 2.0\r
311      */\r
312     public void setParseIntegerOnly(boolean value) {\r
313         numberFormat.setParseIntegerOnly(value);\r
314     }\r
315         \r
316     //============== Locale Stuff =====================\r
317         \r
318     /**\r
319      * Returns the default number format for the current default locale.\r
320      * The default format is one of the styles provided by the other\r
321      * factory methods: getNumberInstance, getIntegerInstance,\r
322      * getCurrencyInstance or getPercentInstance.\r
323      * Exactly which one is locale-dependent.\r
324      * @stable ICU 2.0\r
325      */\r
326     public final static NumberFormat getInstance() {\r
327         return getInstance(ULocale.getDefault(), NUMBERSTYLE);\r
328     }\r
329         \r
330     /**\r
331      * Returns the default number format for the specified locale.\r
332      * The default format is one of the styles provided by the other\r
333      * factory methods: getNumberInstance, getCurrencyInstance or getPercentInstance.\r
334      * Exactly which one is locale-dependent.\r
335      * @stable ICU 2.0\r
336      */\r
337     public static NumberFormat getInstance(Locale inLocale) {\r
338         return getInstance(ULocale.forLocale(inLocale), NUMBERSTYLE);\r
339     }\r
340         \r
341     /**\r
342      * Returns the default number format for the specified locale.\r
343      * The default format is one of the styles provided by the other\r
344      * factory methods: getNumberInstance, getCurrencyInstance or getPercentInstance.\r
345      * Exactly which one is locale-dependent.\r
346      * @stable ICU 3.2\r
347      */\r
348     public static NumberFormat getInstance(ULocale inLocale) {\r
349         return getInstance(inLocale, NUMBERSTYLE);\r
350     }\r
351         \r
352     /**\r
353      * Returns a general-purpose number format for the current default locale.\r
354      * @stable ICU 2.0\r
355      */\r
356     public final static NumberFormat getNumberInstance() {\r
357         return getInstance(ULocale.getDefault(), NUMBERSTYLE);\r
358     }\r
359         \r
360     /**\r
361      * Returns a general-purpose number format for the specified locale.\r
362      * @stable ICU 2.0\r
363      */\r
364     public static NumberFormat getNumberInstance(Locale inLocale) {\r
365         return getInstance(ULocale.forLocale(inLocale), NUMBERSTYLE);\r
366     }\r
367         \r
368     /**\r
369      * Returns a general-purpose number format for the specified locale.\r
370      * @stable ICU 3.2\r
371      */\r
372     public static NumberFormat getNumberInstance(ULocale inLocale) {\r
373         return getInstance(inLocale, NUMBERSTYLE);\r
374     }\r
375         \r
376     /**\r
377      * Returns an integer number format for the current default locale. The\r
378      * returned number format is configured to round floating point numbers\r
379      * to the nearest integer using IEEE half-even rounding (see {@link \r
380      * com.ibm.icu.math.BigDecimal#ROUND_HALF_EVEN ROUND_HALF_EVEN}) for formatting,\r
381      * and to parse only the integer part of an input string (see {@link\r
382      * #isParseIntegerOnly isParseIntegerOnly}).\r
383      *\r
384      * @return a number format for integer values\r
385      * @stable ICU 2.0\r
386      */\r
387     public final static NumberFormat getIntegerInstance() {\r
388         return getInstance(ULocale.getDefault(), INTEGERSTYLE);\r
389     }\r
390         \r
391     /**\r
392      * Returns an integer number format for the specified locale. The\r
393      * returned number format is configured to round floating point numbers\r
394      * to the nearest integer using IEEE half-even rounding (see {@link \r
395      * com.ibm.icu.math.BigDecimal#ROUND_HALF_EVEN ROUND_HALF_EVEN}) for formatting,\r
396      * and to parse only the integer part of an input string (see {@link\r
397      * #isParseIntegerOnly isParseIntegerOnly}).\r
398      *\r
399      * @param inLocale the locale for which a number format is needed\r
400      * @return a number format for integer values\r
401      * @stable ICU 2.0\r
402      */\r
403     public static NumberFormat getIntegerInstance(Locale inLocale) {\r
404         return getInstance(ULocale.forLocale(inLocale), INTEGERSTYLE);\r
405     }\r
406         \r
407     /**\r
408      * Returns an integer number format for the specified locale. The\r
409      * returned number format is configured to round floating point numbers\r
410      * to the nearest integer using IEEE half-even rounding (see {@link \r
411      * com.ibm.icu.math.BigDecimal#ROUND_HALF_EVEN ROUND_HALF_EVEN}) for formatting,\r
412      * and to parse only the integer part of an input string (see {@link\r
413      * #isParseIntegerOnly isParseIntegerOnly}).\r
414      *\r
415      * @param inLocale the locale for which a number format is needed\r
416      * @return a number format for integer values\r
417      * @stable ICU 3.2\r
418      */\r
419     public static NumberFormat getIntegerInstance(ULocale inLocale) {\r
420         return getInstance(inLocale, INTEGERSTYLE);\r
421     }\r
422         \r
423     /**\r
424      * Returns a currency format for the current default locale.\r
425      * @return a number format for currency\r
426      * @stable ICU 2.0\r
427      */\r
428     public final static NumberFormat getCurrencyInstance() {\r
429         return getInstance(ULocale.getDefault(), CURRENCYSTYLE);\r
430     }\r
431         \r
432     /**\r
433      * Returns a currency format for the specified locale.\r
434      * @return a number format for currency\r
435      * @stable ICU 2.0\r
436      */\r
437     public static NumberFormat getCurrencyInstance(Locale inLocale) {\r
438         return getInstance(ULocale.forLocale(inLocale), CURRENCYSTYLE);\r
439     }\r
440         \r
441     /**\r
442      * Returns a currency format for the specified locale.\r
443      * @return a number format for currency\r
444      * @stable ICU 3.2\r
445      */\r
446     public static NumberFormat getCurrencyInstance(ULocale inLocale) {\r
447         return getInstance(inLocale, CURRENCYSTYLE);\r
448     }\r
449         \r
450     /**\r
451      * Returns a percentage format for the current default locale.\r
452      * @return a number format for percents\r
453      * @stable ICU 2.0\r
454      */\r
455     public final static NumberFormat getPercentInstance() {\r
456         return getInstance(ULocale.getDefault(), PERCENTSTYLE);\r
457     }\r
458         \r
459     /**\r
460      * Returns a percentage format for the specified locale.\r
461      * @return a number format for percents\r
462      * @stable ICU 2.0\r
463      */\r
464     public static NumberFormat getPercentInstance(Locale inLocale) {\r
465         return getInstance(ULocale.forLocale(inLocale), PERCENTSTYLE);\r
466     }\r
467         \r
468     /**\r
469      * Returns a percentage format for the specified locale.\r
470      * @return a number format for percents\r
471      * @stable ICU 3.2\r
472      */\r
473     public static NumberFormat getPercentInstance(ULocale inLocale) {\r
474         return getInstance(inLocale, PERCENTSTYLE);\r
475     }\r
476         \r
477     /**\r
478      * <strong><font face=helvetica color=red>NEW</font></strong>\r
479      * Returns a scientific format for the current default locale.\r
480      * @return a scientific number format\r
481      * @stable ICU 2.0\r
482      */\r
483     public final static NumberFormat getScientificInstance() {\r
484         return getInstance(ULocale.getDefault(), SCIENTIFICSTYLE);\r
485     }\r
486         \r
487     /**\r
488      * <strong><font face=helvetica color=red>NEW</font></strong>\r
489      * Returns a scientific format for the specified locale.\r
490      * @return a scientific number format\r
491      * @stable ICU 2.0\r
492      */\r
493     public static NumberFormat getScientificInstance(Locale inLocale) {\r
494         return getInstance(ULocale.forLocale(inLocale), SCIENTIFICSTYLE);\r
495     }\r
496         \r
497     /**\r
498      * <strong><font face=helvetica color=red>NEW</font></strong>\r
499      * Returns a scientific format for the specified locale.\r
500      * @return a scientific number format\r
501      * @stable ICU 3.2\r
502      */\r
503     public static NumberFormat getScientificInstance(ULocale inLocale) {\r
504         return getInstance(inLocale, SCIENTIFICSTYLE);\r
505     }\r
506         \r
507     /**\r
508      * Get the list of Locales for which NumberFormats are available.\r
509      * @return the available locales\r
510      * @stable ICU 2.0\r
511      */\r
512     public static Locale[] getAvailableLocales() {\r
513         return java.text.NumberFormat.getAvailableLocales();\r
514     }\r
515         \r
516     /**\r
517      * Get the list of Locales for which NumberFormats are available.\r
518      * @return the available locales\r
519      * @draft ICU 3.2 (retain)\r
520      */\r
521     public static ULocale[] getAvailableULocales() {\r
522         if (availableULocales == null) {\r
523             Locale[] locales = java.text.NumberFormat.getAvailableLocales();\r
524             ULocale[] ulocales = new ULocale[locales.length];\r
525             for (int i = 0; i < locales.length; ++i) {\r
526                 ulocales[i] = ULocale.forLocale(locales[i]);\r
527             }\r
528             availableULocales = ulocales;\r
529         }\r
530         return (ULocale[])availableULocales.clone();\r
531     }\r
532     private static ULocale[] availableULocales;\r
533         \r
534         \r
535     /**\r
536      * Returns true if grouping is used in this format. For example, in the\r
537      * en_US locale, with grouping on, the number 1234567 will be formatted\r
538      * as "1,234,567". The grouping separator as well as the size of each group\r
539      * is locale-dependent and is determined by subclasses of NumberFormat.\r
540      * Grouping affects both parsing and formatting.\r
541      * @return true if grouping is used\r
542      * @see #setGroupingUsed\r
543      * @stable ICU 2.0\r
544      */\r
545     public boolean isGroupingUsed() {\r
546         return numberFormat.isGroupingUsed();\r
547     }\r
548         \r
549     /**\r
550      * Sets whether or not grouping will be used in this format.  Grouping\r
551      * affects both parsing and formatting.\r
552      * @see #isGroupingUsed\r
553      * @param newValue true to use grouping.\r
554      * @stable ICU 2.0\r
555      */\r
556     public void setGroupingUsed(boolean newValue) {\r
557         numberFormat.setGroupingUsed(newValue);\r
558     }\r
559         \r
560     /**\r
561      * Returns the maximum number of digits allowed in the integer portion of a\r
562      * number.  The default value is 40, which subclasses can override.\r
563      * When formatting, the exact behavior when this value is exceeded is\r
564      * subclass-specific.  When parsing, this has no effect.\r
565      * @return the maximum number of integer digits\r
566      * @see #setMaximumIntegerDigits\r
567      * @stable ICU 2.0\r
568      */\r
569     public int getMaximumIntegerDigits() {\r
570         return numberFormat.getMaximumIntegerDigits();\r
571     }\r
572         \r
573     /**\r
574      * Sets the maximum number of digits allowed in the integer portion of a\r
575      * number. This must be >= minimumIntegerDigits.  If the\r
576      * new value for maximumIntegerDigits is less than the current value\r
577      * of minimumIntegerDigits, then minimumIntegerDigits will also be set to\r
578      * the new value.\r
579      * @param newValue the maximum number of integer digits to be shown; if\r
580      * less than zero, then zero is used.  Subclasses might enforce an\r
581      * upper limit to this value appropriate to the numeric type being formatted.\r
582      * @see #getMaximumIntegerDigits\r
583      * @stable ICU 2.0\r
584      */\r
585     public void setMaximumIntegerDigits(int newValue) {\r
586         numberFormat.setMaximumIntegerDigits(newValue);\r
587     }\r
588         \r
589     /**\r
590      * Returns the minimum number of digits allowed in the integer portion of a\r
591      * number.  The default value is 1, which subclasses can override.\r
592      * When formatting, if this value is not reached, numbers are padded on the\r
593      * left with the locale-specific '0' character to ensure at least this\r
594      * number of integer digits.  When parsing, this has no effect.\r
595      * @return the minimum number of integer digits\r
596      * @see #setMinimumIntegerDigits\r
597      * @stable ICU 2.0\r
598      */\r
599     public int getMinimumIntegerDigits() {\r
600         return numberFormat.getMinimumIntegerDigits();\r
601     }\r
602         \r
603     /**\r
604      * Sets the minimum number of digits allowed in the integer portion of a\r
605      * number.  This must be <= maximumIntegerDigits.  If the\r
606      * new value for minimumIntegerDigits is more than the current value\r
607      * of maximumIntegerDigits, then maximumIntegerDigits will also be set to\r
608      * the new value.\r
609      * @param newValue the minimum number of integer digits to be shown; if\r
610      * less than zero, then zero is used. Subclasses might enforce an\r
611      * upper limit to this value appropriate to the numeric type being formatted.\r
612      * @see #getMinimumIntegerDigits\r
613      * @stable ICU 2.0\r
614      */\r
615     public void setMinimumIntegerDigits(int newValue) {\r
616         numberFormat.setMinimumIntegerDigits(newValue);\r
617     }\r
618         \r
619     /**\r
620      * Returns the maximum number of digits allowed in the fraction\r
621      * portion of a number.  The default value is 3, which subclasses\r
622      * can override.  When formatting, the exact behavior when this\r
623      * value is exceeded is subclass-specific.  When parsing, this has \r
624      * no effect.\r
625      * @return the maximum number of fraction digits\r
626      * @see #setMaximumFractionDigits\r
627      * @stable ICU 2.0 \r
628      */\r
629     public int getMaximumFractionDigits() {\r
630         return numberFormat.getMaximumFractionDigits();\r
631     }\r
632         \r
633     /**\r
634      * Sets the maximum number of digits allowed in the fraction portion of a\r
635      * number. This must be >= minimumFractionDigits.  If the\r
636      * new value for maximumFractionDigits is less than the current value\r
637      * of minimumFractionDigits, then minimumFractionDigits will also be set to\r
638      * the new value.\r
639      * @param newValue the maximum number of fraction digits to be shown; if\r
640      * less than zero, then zero is used. The concrete subclass may enforce an\r
641      * upper limit to this value appropriate to the numeric type being formatted.\r
642      * @see #getMaximumFractionDigits\r
643      * @stable ICU 2.0\r
644      */\r
645     public void setMaximumFractionDigits(int newValue) {\r
646         numberFormat.setMaximumFractionDigits(newValue);\r
647     }\r
648         \r
649     /**\r
650      * Returns the minimum number of digits allowed in the fraction portion of a\r
651      * number.  The default value is 0, which subclasses can override.\r
652      * When formatting, if this value is not reached, numbers are padded on\r
653      * the right with the locale-specific '0' character to ensure at least\r
654      * this number of fraction digits.  When parsing, this has no effect.\r
655      * @return the minimum number of fraction digits\r
656      * @see #setMinimumFractionDigits\r
657      * @stable ICU 2.0\r
658      */\r
659     public int getMinimumFractionDigits() {\r
660         return numberFormat.getMinimumFractionDigits();\r
661     }\r
662         \r
663     /**\r
664      * Sets the minimum number of digits allowed in the fraction portion of a\r
665      * number.  This must be <= maximumFractionDigits.  If the\r
666      * new value for minimumFractionDigits exceeds the current value\r
667      * of maximumFractionDigits, then maximumFractionDigits will also be set to\r
668      * the new value.\r
669      * @param newValue the minimum number of fraction digits to be shown; if\r
670      * less than zero, then zero is used.  Subclasses might enforce an\r
671      * upper limit to this value appropriate to the numeric type being formatted.\r
672      * @see #getMinimumFractionDigits\r
673      * @stable ICU 2.0\r
674      */\r
675     public void setMinimumFractionDigits(int newValue) {\r
676         numberFormat.setMinimumFractionDigits(newValue);\r
677     }\r
678 \r
679     /**\r
680      * Return a string suitable for debugging.\r
681      * @return a string suitable for debugging\r
682      * @stable ICU 3.4.2\r
683      */\r
684     public String toString() {\r
685         return numberFormat.toString();\r
686     }\r
687         \r
688     /**\r
689      * Overrides Cloneable.\r
690      * @stable ICU 2.0\r
691      */\r
692     public Object clone() {\r
693         return new NumberFormat((java.text.NumberFormat)numberFormat.clone());\r
694     }\r
695         \r
696     /**\r
697      * Overrides equals.  Two NumberFormats are equal if they are of the same class\r
698      * and the settings (groupingUsed, parseIntegerOnly, maximumIntegerDigits, etc.\r
699      * are equal.\r
700      * @param obj the object to compare against\r
701      * @return true if the object is equal to this.\r
702      * @stable ICU 2.0\r
703      */\r
704     public boolean equals(Object obj) {\r
705         try {\r
706             return numberFormat.equals(((NumberFormat)obj).numberFormat);\r
707         }\r
708         catch (Exception e) {\r
709             return false;\r
710         }\r
711     }\r
712         \r
713     /**\r
714      * Overrides hashCode\r
715      * @stable ICU 2.0\r
716      */\r
717     public int hashCode() {\r
718         return numberFormat.hashCode();\r
719     }\r
720 \r
721     // =======================privates===============================\r
722         \r
723     private static NumberFormat getInstance(ULocale desiredLocale, int choice) {\r
724         Locale locale = desiredLocale.toLocale();\r
725         java.text.NumberFormat nf = null;\r
726         switch (choice) {\r
727         case NUMBERSTYLE: nf = java.text.NumberFormat.getInstance(locale); break;\r
728         case CURRENCYSTYLE: nf = java.text.NumberFormat.getCurrencyInstance(locale); break;\r
729         case PERCENTSTYLE: nf = java.text.NumberFormat.getPercentInstance(locale); break;\r
730         case SCIENTIFICSTYLE: nf = new java.text.DecimalFormat("#E0", new DecimalFormatSymbols(locale)); \r
731             nf.setMaximumFractionDigits(10);\r
732             break;\r
733         case INTEGERSTYLE: \r
734             if (unchecked) {\r
735                 unchecked = false;\r
736                 try {\r
737                     Class[] args = { java.util.Locale.class };\r
738                     integer14API = java.text.NumberFormat.class.getMethod("getIntegerInstance", args);\r
739                 }\r
740                 catch (Exception e) {\r
741                 }\r
742             }\r
743             if (integer14API != null) {\r
744                 try {\r
745                     Object[] args = { locale };\r
746                     nf = (java.text.NumberFormat)integer14API.invoke(null, args);\r
747                 }\r
748                 catch (IllegalAccessException e) {\r
749                     integer14API = null;\r
750                 }\r
751                 catch (InvocationTargetException e) {\r
752                     integer14API = null;\r
753                 }\r
754                 catch (Exception e) {\r
755                     // shouldn't happen, but locale might be null, for example\r
756                     // and we don't want to throw away our method because someone\r
757                     // called us with a bad parameter\r
758                 }\r
759             }\r
760             if (nf == null) {\r
761                 nf = java.text.NumberFormat.getNumberInstance(locale); \r
762                 nf.setMaximumFractionDigits(0);\r
763                 nf.setParseIntegerOnly(true);\r
764             }\r
765             break;\r
766         }\r
767         return new NumberFormat(nf);\r
768     }\r
769     \r
770     private static boolean unchecked = true;\r
771     private static Method integer14API;\r
772     \r
773     private static final int NUMBERSTYLE = 0;\r
774     private static final int CURRENCYSTYLE = 1;\r
775     private static final int PERCENTSTYLE = 2;\r
776     private static final int SCIENTIFICSTYLE = 3;\r
777     private static final int INTEGERSTYLE = 4;\r
778 }\r