DecimalFormat.java revision 34a3a0501220dd62b88ddb4a355bad46b15c25a2
1f33eae7e84eb6d3b0f4e86b59605bb3de73009f3Elliott Hughes/*
2adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * Licensed to the Apache Software Foundation (ASF) under one or more
3adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * contributor license agreements.  See the NOTICE file distributed with
4adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * this work for additional information regarding copyright ownership.
5adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * The ASF licenses this file to You under the Apache License, Version 2.0
6adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * (the "License"); you may not use this file except in compliance with
7adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * the License.  You may obtain a copy of the License at
8f33eae7e84eb6d3b0f4e86b59605bb3de73009f3Elliott Hughes *
9adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project *     http://www.apache.org/licenses/LICENSE-2.0
10f33eae7e84eb6d3b0f4e86b59605bb3de73009f3Elliott Hughes *
11adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * Unless required by applicable law or agreed to in writing, software
12adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * distributed under the License is distributed on an "AS IS" BASIS,
13adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * See the License for the specific language governing permissions and
15adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * limitations under the License.
16adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project */
17adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
18adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Projectpackage java.text;
19adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
20adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Projectimport java.io.IOException;
21adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Projectimport java.io.ObjectInputStream;
22adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Projectimport java.io.ObjectOutputStream;
23adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Projectimport java.io.ObjectStreamField;
24adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Projectimport java.math.BigDecimal;
25adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Projectimport java.math.BigInteger;
2642db7d138547e3bba75cf6ed02b6a319e30004aeElliott Hughesimport java.math.RoundingMode;
27adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Projectimport java.util.Currency;
28adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Projectimport java.util.Locale;
29162b0775772fa66b7eb634760a8159a60c1ddceaElliott Hughesimport libcore.icu.LocaleData;
30c27a366a89e470690e99374b15270e7b9169ade1Elliott Hughesimport libcore.icu.NativeDecimalFormat;
31adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
32adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project/**
33adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * A concrete subclass of {@link NumberFormat} that formats decimal numbers. It
34adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * has a variety of features designed to make it possible to parse and format
35adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * numbers in any locale, including support for Western, Arabic, or Indic
36adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * digits. It also supports different flavors of numbers, including integers
37adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * ("123"), fixed-point numbers ("123.4"), scientific notation ("1.23E4"),
38adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * percentages ("12%"), and currency amounts ("$123"). All of these flavors can
39adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * be easily localized.
40adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <p>
41adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <strong>This is an enhanced version of {@code DecimalFormat} that is based on
42adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * the standard version in the RI. New or changed functionality is labeled
43adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <strong><font color="red">NEW</font></strong>.</strong>
44adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <p>
45adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * To obtain a {@link NumberFormat} for a specific locale (including the default
46adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * locale), call one of {@code NumberFormat}'s factory methods such as
47adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * {@code NumberFormat.getInstance}. Do not call the {@code DecimalFormat}
48adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * constructors directly, unless you know what you are doing, since the
49adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * {@link NumberFormat} factory methods may return subclasses other than
50adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * {@code DecimalFormat}. If you need to customize the format object, do
51adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * something like this: <blockquote>
529b354e75f2418e54638e02d153216660b67d01b8Jesse Wilson *
53adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <pre>
54adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * NumberFormat f = NumberFormat.getInstance(loc);
55adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * if (f instanceof DecimalFormat) {
56adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project *     ((DecimalFormat)f).setDecimalSeparatorAlwaysShown(true);
57adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * }
58adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * </pre>
599b354e75f2418e54638e02d153216660b67d01b8Jesse Wilson *
60adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * </blockquote>
619b354e75f2418e54638e02d153216660b67d01b8Jesse Wilson *
62adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <h4>Patterns</h4>
63adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <p>
64adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * A {@code DecimalFormat} consists of a <em>pattern</em> and a set of
65adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <em>symbols</em>. The pattern may be set directly using
66adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * {@link #applyPattern(String)}, or indirectly using other API methods which
67adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * manipulate aspects of the pattern, such as the minimum number of integer
68adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * digits. The symbols are stored in a {@link DecimalFormatSymbols} object. When
69adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * using the {@link NumberFormat} factory methods, the pattern and symbols are
70adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * read from ICU's locale data.
71adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <h4>Special Pattern Characters</h4>
72adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <p>
73adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * Many characters in a pattern are taken literally; they are matched during
74adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * parsing and are written out unchanged during formatting. On the other hand,
75adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * special characters stand for other characters, strings, or classes of
76adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * characters. For example, the '#' character is replaced by a localized digit.
77adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * Often the replacement character is the same as the pattern character; in the
78adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * U.S. locale, the ',' grouping character is replaced by ','. However, the
79adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * replacement is still happening, and if the symbols are modified, the grouping
80adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * character changes. Some special characters affect the behavior of the
81adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * formatter by their presence; for example, if the percent character is seen,
82adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * then the value is multiplied by 100 before being displayed.
83adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <p>
84adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * To insert a special character in a pattern as a literal, that is, without any
85adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * special meaning, the character must be quoted. There are some exceptions to
86adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * this which are noted below.
87adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <p>
88adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * The characters listed here are used in non-localized patterns. Localized
89adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * patterns use the corresponding characters taken from this formatter's
90adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * {@link DecimalFormatSymbols} object instead, and these characters lose their
91adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * special status. Two exceptions are the currency sign and quote, which are not
92adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * localized.
93adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <blockquote> <table border="0" cellspacing="3" cellpadding="0" summary="Chart
94adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * showing symbol, location, localized, and meaning.">
95adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <tr bgcolor="#ccccff">
96adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <th align="left">Symbol</th>
97adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <th align="left">Location</th>
98adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <th align="left">Localized?</th>
99adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <th align="left">Meaning</th>
100adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * </tr>
101adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <tr valign="top">
102adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <td>{@code 0}</td>
103adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <td>Number</td>
104adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <td>Yes</td>
105adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <td>Digit.</td>
106adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * </tr>
107adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <tr valign="top">
108adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <td>{@code @}</td>
109adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <td>Number</td>
110adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <td>No</td>
111adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <td><strong><font color="red">NEW</font>&nbsp;</strong> Significant
112adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * digit.</td>
113adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * </tr>
114adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <tr valign="top" bgcolor="#eeeeff">
115adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <td>{@code #}</td>
116adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <td>Number</td>
117adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <td>Yes</td>
118adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <td>Digit, leading zeroes are not shown.</td>
119adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * </tr>
120adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <tr valign="top">
121adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <td>{@code .}</td>
122adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <td>Number</td>
123adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <td>Yes</td>
124adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <td>Decimal separator or monetary decimal separator.</td>
125adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * </tr>
126adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <tr valign="top" bgcolor="#eeeeff">
127adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <td>{@code -}</td>
128adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <td>Number</td>
129adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <td>Yes</td>
130adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <td>Minus sign.</td>
131adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * </tr>
132adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <tr valign="top">
133adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <td>{@code ,}</td>
134adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <td>Number</td>
135adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <td>Yes</td>
136adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <td>Grouping separator.</td>
137adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * </tr>
138adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <tr valign="top" bgcolor="#eeeeff">
139adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <td>{@code E}</td>
140adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <td>Number</td>
141adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <td>Yes</td>
142adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <td>Separates mantissa and exponent in scientific notation.
143adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <em>Does not need to be quoted in prefix or suffix.</em></td>
144adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * </tr>
145adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <tr valign="top">
146adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <td>{@code +}</td>
147adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <td>Exponent</td>
148adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <td>Yes</td>
149adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <td><strong><font color="red">NEW</font>&nbsp;</strong> Prefix
150adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * positive exponents with localized plus sign.
151adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <em>Does not need to be quoted in prefix or suffix.</em></td>
152adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * </tr>
153adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <tr valign="top" bgcolor="#eeeeff">
154adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <td>{@code ;}</td>
155adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <td>Subpattern boundary</td>
156adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <td>Yes</td>
157adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <td>Separates positive and negative subpatterns.</td>
158adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * </tr>
159adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <tr valign="top">
160adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <td>{@code %}</td>
161adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <td>Prefix or suffix</td>
162adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <td>Yes</td>
163adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <td>Multiply by 100 and show as percentage.</td>
164adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * </tr>
165adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <tr valign="top" bgcolor="#eeeeff">
1661d58625189328f29c10fa98d52512ee6cf8cc641Ben Dodson * <td>{@code \u2030} ({@code \u005Cu2030})</td>
167adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <td>Prefix or suffix</td>
168adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <td>Yes</td>
169adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <td>Multiply by 1000 and show as per mille.</td>
170adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * </tr>
171adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <tr valign="top">
1721d58625189328f29c10fa98d52512ee6cf8cc641Ben Dodson * <td>{@code \u00A4} ({@code \u005Cu00A4})</td>
173adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <td>Prefix or suffix</td>
174adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <td>No</td>
175adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <td>Currency sign, replaced by currency symbol. If doubled, replaced by
176adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * international currency symbol. If present in a pattern, the monetary decimal
177adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * separator is used instead of the decimal separator.</td>
178adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * </tr>
179adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <tr valign="top" bgcolor="#eeeeff">
180adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <td>{@code '}</td>
181adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <td>Prefix or suffix</td>
182adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <td>No</td>
183adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <td>Used to quote special characters in a prefix or suffix, for example,
184adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * {@code "'#'#"} formats 123 to {@code "#123"}. To create a single quote
185adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * itself, use two in a row: {@code "# o''clock"}.</td>
186adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * </tr>
187adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <tr valign="top">
188adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <td>{@code *}</td>
189adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <td>Prefix or suffix boundary</td>
190adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <td>Yes</td>
191adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <td><strong><font color="red">NEW</font>&nbsp;</strong> Pad escape,
192adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * precedes pad character. </td>
193adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * </tr>
194adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * </table> </blockquote>
195adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <p>
196757a7942eed2b0aa457f8517a0259d2ac82c5b18Elliott Hughes * A {@code DecimalFormat} pattern contains a positive and negative subpattern,
197adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * for example, "#,##0.00;(#,##0.00)". Each subpattern has a prefix, a numeric
198adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * part and a suffix. If there is no explicit negative subpattern, the negative
199adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * subpattern is the localized minus sign prefixed to the positive subpattern.
200adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * That is, "0.00" alone is equivalent to "0.00;-0.00". If there is an explicit
201adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * negative subpattern, it serves only to specify the negative prefix and
202adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * suffix; the number of digits, minimal digits, and other characteristics are
203adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * ignored in the negative subpattern. This means that "#,##0.0#;(#)" produces
204adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * precisely the same result as "#,##0.0#;(#,##0.0#)".
205adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <p>
206adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * The prefixes, suffixes, and various symbols used for infinity, digits,
207adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * thousands separators, decimal separators, etc. may be set to arbitrary
208adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * values, and they will appear properly during formatting. However, care must
209adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * be taken that the symbols and strings do not conflict, or parsing will be
210adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * unreliable. For example, either the positive and negative prefixes or the
211adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * suffixes must be distinct for {@link #parse} to be able to distinguish
212adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * positive from negative values. Another example is that the decimal separator
213adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * and thousands separator should be distinct characters, or parsing will be
214adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * impossible.
215adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <p>
216adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * The <em>grouping separator</em> is a character that separates clusters of
217adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * integer digits to make large numbers more legible. It is commonly used for
218adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * thousands, but in some locales it separates ten-thousands. The <em>grouping
219adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * size</em>
220adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * is the number of digits between the grouping separators, such as 3 for
221adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * "100,000,000" or 4 for "1 0000 0000". There are actually two different
222adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * grouping sizes: One used for the least significant integer digits, the
223adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <em>primary grouping size</em>, and one used for all others, the
224adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <em>secondary grouping size</em>. In most locales these are the same, but
225adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * sometimes they are different. For example, if the primary grouping interval
226adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * is 3, and the secondary is 2, then this corresponds to the pattern
227adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * "#,##,##0", and the number 123456789 is formatted as "12,34,56,789". If a
228adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * pattern contains multiple grouping separators, the interval between the last
229adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * one and the end of the integer defines the primary grouping size, and the
230adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * interval between the last two defines the secondary grouping size. All others
231adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * are ignored, so "#,##,###,####", "###,###,####" and "##,#,###,####" produce
232adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * the same result.
233adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <p>
234adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * Illegal patterns, such as "#.#.#" or "#.###,###", will cause
235adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * {@code DecimalFormat} to throw an {@link IllegalArgumentException} with a
236adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * message that describes the problem.
237adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <h4>Pattern BNF</h4>
2389b354e75f2418e54638e02d153216660b67d01b8Jesse Wilson *
239adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <pre>
240adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * pattern    := subpattern (';' subpattern)?
241adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * subpattern := prefix? number exponent? suffix?
242adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * number     := (integer ('.' fraction)?) | sigDigits
243adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * prefix     := '\\u0000'..'\\uFFFD' - specialCharacters
244adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * suffix     := '\\u0000'..'\\uFFFD' - specialCharacters
245adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * integer    := '#'* '0'* '0'
246adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * fraction   := '0'* '#'*
247adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * sigDigits  := '#'* '@' '@'* '#'*
248adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * exponent   := 'E' '+'? '0'* '0'
249adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * padSpec    := '*' padChar
250adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * padChar    := '\\u0000'..'\\uFFFD' - quote
2519b354e75f2418e54638e02d153216660b67d01b8Jesse Wilson *
252adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * Notation:
253adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project *   X*       0 or more instances of X
254adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project *   X?       0 or 1 instances of X
255adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project *   X|Y      either X or Y
256adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project *   C..D     any character from C up to D, inclusive
257adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project *   S-T      characters in S, except those in T
258adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * </pre>
2599b354e75f2418e54638e02d153216660b67d01b8Jesse Wilson *
260adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * The first subpattern is for positive numbers. The second (optional)
261adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * subpattern is for negative numbers.
262adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <p>
263adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * Not indicated in the BNF syntax above:
264adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <ul>
265adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <li>The grouping separator ',' can occur inside the integer and sigDigits
266adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * elements, between any two pattern characters of that element, as long as the
267adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * integer or sigDigits element is not followed by the exponent element.
268adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <li><font color="red"><strong>NEW</strong>&nbsp;</font> Two
269adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * grouping intervals are recognized: The one between the decimal point and the
270adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * first grouping symbol and the one between the first and second grouping
271adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * symbols. These intervals are identical in most locales, but in some locales
272adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * they differ. For example, the pattern &quot;#,##,###&quot; formats the number
273adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * 123456789 as &quot;12,34,56,789&quot;.</li>
274adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <li> <strong><font color="red">NEW</font>&nbsp;</strong> The pad
275adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * specifier {@code padSpec} may appear before the prefix, after the prefix,
276adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * before the suffix, after the suffix or not at all.
277adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * </ul>
278adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <h4>Parsing</h4>
279adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <p>
280adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * {@code DecimalFormat} parses all Unicode characters that represent decimal
281adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * digits, as defined by {@link Character#digit(int, int)}. In addition,
282adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * {@code DecimalFormat} also recognizes as digits the ten consecutive
283adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * characters starting with the localized zero digit defined in the
284adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * {@link DecimalFormatSymbols} object. During formatting, the
285adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * {@link DecimalFormatSymbols}-based digits are written out.
286adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <p>
287adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * During parsing, grouping separators are ignored.
288adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <p>
289adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * If {@link #parse(String, ParsePosition)} fails to parse a string, it returns
290adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * {@code null} and leaves the parse position unchanged.
291adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <h4>Formatting</h4>
292adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <p>
293adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * Formatting is guided by several parameters, all of which can be specified
294adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * either using a pattern or using the API. The following description applies to
295adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * formats that do not use <a href="#sci">scientific notation</a> or <a
296adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * href="#sigdig">significant digits</a>.
297adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <ul>
298adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <li>If the number of actual integer digits exceeds the
299adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <em>maximum integer digits</em>, then only the least significant digits
300adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * are shown. For example, 1997 is formatted as "97" if maximum integer digits
301adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * is set to 2.
302adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <li>If the number of actual integer digits is less than the
303adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <em>minimum integer digits</em>, then leading zeros are added. For
304adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * example, 1997 is formatted as "01997" if minimum integer digits is set to 5.
305adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <li>If the number of actual fraction digits exceeds the <em>maximum
306adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * fraction digits</em>,
307adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * then half-even rounding is performed to the maximum fraction digits. For
308adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * example, 0.125 is formatted as "0.12" if the maximum fraction digits is 2.
309adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <li>If the number of actual fraction digits is less than the
310adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <em>minimum fraction digits</em>, then trailing zeros are added. For
311757a7942eed2b0aa457f8517a0259d2ac82c5b18Elliott Hughes * example, 0.125 is formatted as "0.1250" if the minimum fraction digits is set
312adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * to 4.
313adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <li>Trailing fractional zeros are not displayed if they occur <em>j</em>
314adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * positions after the decimal, where <em>j</em> is less than the maximum
315adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * fraction digits. For example, 0.10004 is formatted as "0.1" if the maximum
316adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * fraction digits is four or less.
317adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * </ul>
318adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <p>
319adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <strong>Special Values</strong>
320adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <p>
321adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * {@code NaN} is represented as a single character, typically
3221d58625189328f29c10fa98d52512ee6cf8cc641Ben Dodson * {@code \u005cuFFFD}. This character is determined by the
323adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * {@link DecimalFormatSymbols} object. This is the only value for which the
324adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * prefixes and suffixes are not used.
325adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <p>
3261d58625189328f29c10fa98d52512ee6cf8cc641Ben Dodson * Infinity is represented as a single character, typically {@code \u005cu221E},
327adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * with the positive or negative prefixes and suffixes applied. The infinity
328adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * character is determined by the {@link DecimalFormatSymbols} object. <a
329adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * name="sci">
330adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <h4>Scientific Notation</h4>
331adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * </a>
332adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <p>
333adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * Numbers in scientific notation are expressed as the product of a mantissa and
334adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * a power of ten, for example, 1234 can be expressed as 1.234 x 10<sup>3</sup>.
335adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * The mantissa is typically in the half-open interval [1.0, 10.0) or sometimes
336adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * [0.0, 1.0), but it does not need to be. {@code DecimalFormat} supports
337adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * arbitrary mantissas. {@code DecimalFormat} can be instructed to use
338adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * scientific notation through the API or through the pattern. In a pattern, the
339adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * exponent character immediately followed by one or more digit characters
340adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * indicates scientific notation. Example: "0.###E0" formats the number 1234 as
341adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * "1.234E3".
342adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <ul>
343adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <li>The number of digit characters after the exponent character gives the
344adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * minimum exponent digit count. There is no maximum. Negative exponents are
345adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * formatted using the localized minus sign, <em>not</em> the prefix and
346adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * suffix from the pattern. This allows patterns such as "0.###E0 m/s". To
347adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * prefix positive exponents with a localized plus sign, specify '+' between the
348adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * exponent and the digits: "0.###E+0" will produce formats "1E+1", "1E+0",
349adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * "1E-1", etc. (In localized patterns, use the localized plus sign rather than
350adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * '+'.)
351adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <li>The minimum number of integer digits is achieved by adjusting the
352adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * exponent. Example: 0.00123 formatted with "00.###E0" yields "12.3E-4". This
353adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * only happens if there is no maximum number of integer digits. If there is a
354adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * maximum, then the minimum number of integer digits is fixed at one.
355adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <li>The maximum number of integer digits, if present, specifies the exponent
356adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * grouping. The most common use of this is to generate <em>engineering
357adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * notation</em>,
358adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * in which the exponent is a multiple of three, e.g., "##0.###E0". The number
359adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * 12345 is formatted using "##0.###E0" as "12.345E3".
360adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <li>When using scientific notation, the formatter controls the digit counts
361adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * using significant digits logic. The maximum number of significant digits
362adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * limits the total number of integer and fraction digits that will be shown in
363adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * the mantissa; it does not affect parsing. For example, 12345 formatted with
364adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * "##0.##E0" is "12.3E3". See the section on significant digits for more
365adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * details.
366adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <li>The number of significant digits shown is determined as follows: If no
367adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * significant digits are used in the pattern then the minimum number of
368adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * significant digits shown is one, the maximum number of significant digits
369adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * shown is the sum of the <em>minimum integer</em> and
370adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <em>maximum fraction</em> digits, and it is unaffected by the maximum
371adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * integer digits. If this sum is zero, then all significant digits are shown.
372adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * If significant digits are used in the pattern then the number of integer
373adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * digits is fixed at one and there is no exponent grouping.
374adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <li>Exponential patterns may not contain grouping separators.
375adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * </ul>
376adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <a name="sigdig">
377adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <h4> <strong><font color="red">NEW</font>&nbsp;</strong> Significant
378adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * Digits</h4>
379adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <p>
380adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * </a> {@code DecimalFormat} has two ways of controlling how many digits are
381adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * shown: (a) significant digit counts or (b) integer and fraction digit counts.
382adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * Integer and fraction digit counts are described above. When a formatter uses
383adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * significant digits counts, the number of integer and fraction digits is not
384adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * specified directly, and the formatter settings for these counts are ignored.
385adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * Instead, the formatter uses as many integer and fraction digits as required
386adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * to display the specified number of significant digits.
387adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <h5>Examples:</h5>
388adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <blockquote> <table border=0 cellspacing=3 cellpadding=0>
389adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <tr bgcolor="#ccccff">
390adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <th align="left">Pattern</th>
391adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <th align="left">Minimum significant digits</th>
392adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <th align="left">Maximum significant digits</th>
393adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <th align="left">Number</th>
394adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <th align="left">Output of format()</th>
395adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * </tr>
396adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <tr valign="top">
397adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <td>{@code @@@}
398adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <td>3</td>
399adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <td>3</td>
400adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <td>12345</td>
401adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <td>{@code 12300}</td>
402adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * </tr>
403adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <tr valign="top" bgcolor="#eeeeff">
404adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <td>{@code @@@}</td>
405adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <td>3</td>
406adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <td>3</td>
407adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <td>0.12345</td>
408adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <td>{@code 0.123}</td>
409adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * </tr>
410adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <tr valign="top">
411adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <td>{@code @@##}</td>
412adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <td>2</td>
413adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <td>4</td>
414adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <td>3.14159</td>
415adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <td>{@code 3.142}</td>
416adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * </tr>
417adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <tr valign="top" bgcolor="#eeeeff">
418adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <td>{@code @@##}</td>
419adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <td>2</td>
420adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <td>4</td>
421adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <td>1.23004</td>
422adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <td>{@code 1.23}</td>
423adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * </tr>
424adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * </table> </blockquote>
425adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <ul>
426adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <li>Significant digit counts may be expressed using patterns that specify a
427adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * minimum and maximum number of significant digits. These are indicated by the
428adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * {@code '@'} and {@code '#'} characters. The minimum number of significant
429adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * digits is the number of {@code '@'} characters. The maximum number of
430adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * significant digits is the number of {@code '@'} characters plus the number of
431adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * {@code '#'} characters following on the right. For example, the pattern
432adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * {@code "@@@"} indicates exactly 3 significant digits. The pattern
433adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * {@code "@##"} indicates from 1 to 3 significant digits. Trailing zero digits
434adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * to the right of the decimal separator are suppressed after the minimum number
435adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * of significant digits have been shown. For example, the pattern {@code "@##"}
436adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * formats the number 0.1203 as {@code "0.12"}.
437adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <li>If a pattern uses significant digits, it may not contain a decimal
438adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * separator, nor the {@code '0'} pattern character. Patterns such as
439adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * {@code "@00"} or {@code "@.###"} are disallowed.
440adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <li>Any number of {@code '#'} characters may be prepended to the left of the
441adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * leftmost {@code '@'} character. These have no effect on the minimum and
442adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * maximum significant digit counts, but may be used to position grouping
443adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * separators. For example, {@code "#,#@#"} indicates a minimum of one
444adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * significant digit, a maximum of two significant digits, and a grouping size
445adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * of three.
446adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <li>In order to enable significant digits formatting, use a pattern
447adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * containing the {@code '@'} pattern character.
448adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <li>In order to disable significant digits formatting, use a pattern that
449adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * does not contain the {@code '@'} pattern character.
450adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <li>The number of significant digits has no effect on parsing.
451adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <li>Significant digits may be used together with exponential notation. Such
452adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * patterns are equivalent to a normal exponential pattern with a minimum and
453adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * maximum integer digit count of one, a minimum fraction digit count of the
454adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * number of '@' characters in the pattern - 1, and a maximum fraction digit
455adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * count of the number of '@' and '#' characters in the pattern - 1. For
456adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * example, the pattern {@code "@@###E0"} is equivalent to {@code "0.0###E0"}.
457757a7942eed2b0aa457f8517a0259d2ac82c5b18Elliott Hughes * <li>If significant digits are in use then the integer and fraction digit
458adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * counts, as set via the API, are ignored.
459adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * </ul>
460adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <h4> <strong><font color="red">NEW</font>&nbsp;</strong> Padding</h4>
461adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <p>
462adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * {@code DecimalFormat} supports padding the result of {@code format} to a
463adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * specific width. Padding may be specified either through the API or through
464adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * the pattern syntax. In a pattern, the pad escape character followed by a
465adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * single pad character causes padding to be parsed and formatted. The pad
466adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * escape character is '*' in unlocalized patterns. For example,
467adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * {@code "$*x#,##0.00"} formats 123 to {@code "$xx123.00"}, and 1234 to
468adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * {@code "$1,234.00"}.
469adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <ul>
470adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <li>When padding is in effect, the width of the positive subpattern,
471adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * including prefix and suffix, determines the format width. For example, in the
472adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * pattern {@code "* #0 o''clock"}, the format width is 10.</li>
473adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <li>The width is counted in 16-bit code units (Java {@code char}s).</li>
474adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <li>Some parameters which usually do not matter have meaning when padding is
475adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * used, because the pattern width is significant with padding. In the pattern "*
476adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * ##,##,#,##0.##", the format width is 14. The initial characters "##,##," do
477adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * not affect the grouping size or maximum integer digits, but they do affect
478adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * the format width.</li>
479adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <li>Padding may be inserted at one of four locations: before the prefix,
480adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * after the prefix, before the suffix or after the suffix. If padding is
481adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * specified in any other location, {@link #applyPattern} throws an {@link
482adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * IllegalArgumentException}. If there is no prefix, before the prefix and after
483adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * the prefix are equivalent, likewise for the suffix.</li>
484adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <li>When specified in a pattern, the 16-bit {@code char} immediately
485adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * following the pad escape is the pad character. This may be any character,
486adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * including a special pattern character. That is, the pad escape
487adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <em>escapes</em> the following character. If there is no character after
488adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * the pad escape, then the pattern is illegal.</li>
489adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * </ul>
490adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <h4>Synchronization</h4>
491adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <p>
492adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * {@code DecimalFormat} objects are not synchronized. Multiple threads should
493adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * not access one formatter concurrently.
4949b354e75f2418e54638e02d153216660b67d01b8Jesse Wilson *
495adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * @see Format
496adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * @see NumberFormat
497adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project */
498adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Projectpublic class DecimalFormat extends NumberFormat {
499adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
500adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    private static final long serialVersionUID = 864413376551465018L;
501adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
502adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    private transient DecimalFormatSymbols symbols;
503adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
50434a3a0501220dd62b88ddb4a355bad46b15c25a2Elliott Hughes    private transient NativeDecimalFormat ndf;
505adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
50642db7d138547e3bba75cf6ed02b6a319e30004aeElliott Hughes    private transient RoundingMode roundingMode = RoundingMode.HALF_EVEN;
50742db7d138547e3bba75cf6ed02b6a319e30004aeElliott Hughes
508adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    /**
509adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * Constructs a new {@code DecimalFormat} for formatting and parsing numbers
5103106a99ccbe2e2a25bb66266d0ee42d98dd18099Elliott Hughes     * for the user's default locale.
5113106a99ccbe2e2a25bb66266d0ee42d98dd18099Elliott Hughes     * See "<a href="../util/Locale.html#default_locale">Be wary of the default locale</a>".
512adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     */
513adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    public DecimalFormat() {
5149b354e75f2418e54638e02d153216660b67d01b8Jesse Wilson        Locale locale = Locale.getDefault();
515f2d5062b67e57ef00ee81fec67480f0d58d66b50Elliott Hughes        this.symbols = new DecimalFormatSymbols(locale);
516757a7942eed2b0aa457f8517a0259d2ac82c5b18Elliott Hughes        initNative(LocaleData.get(locale).numberPattern);
517adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    }
518adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
519adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    /**
520adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * Constructs a new {@code DecimalFormat} using the specified non-localized
5213106a99ccbe2e2a25bb66266d0ee42d98dd18099Elliott Hughes     * pattern and the {@code DecimalFormatSymbols} for the user's default Locale.
5223106a99ccbe2e2a25bb66266d0ee42d98dd18099Elliott Hughes     * See "<a href="../util/Locale.html#default_locale">Be wary of the default locale</a>".
523adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * @param pattern
524adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     *            the non-localized pattern.
5259b354e75f2418e54638e02d153216660b67d01b8Jesse Wilson     * @throws IllegalArgumentException
5269b354e75f2418e54638e02d153216660b67d01b8Jesse Wilson     *            if the pattern cannot be parsed.
527adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     */
528adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    public DecimalFormat(String pattern) {
529f2d5062b67e57ef00ee81fec67480f0d58d66b50Elliott Hughes        this(pattern, Locale.getDefault());
530adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    }
531adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
532adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    /**
533adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * Constructs a new {@code DecimalFormat} using the specified non-localized
534adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * pattern and {@code DecimalFormatSymbols}.
535f33eae7e84eb6d3b0f4e86b59605bb3de73009f3Elliott Hughes     *
536adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * @param pattern
537adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     *            the non-localized pattern.
538adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * @param value
539adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     *            the DecimalFormatSymbols.
5409b354e75f2418e54638e02d153216660b67d01b8Jesse Wilson     * @throws IllegalArgumentException
5419b354e75f2418e54638e02d153216660b67d01b8Jesse Wilson     *            if the pattern cannot be parsed.
542adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     */
543adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    public DecimalFormat(String pattern, DecimalFormatSymbols value) {
544f2d5062b67e57ef00ee81fec67480f0d58d66b50Elliott Hughes        this.symbols = (DecimalFormatSymbols) value.clone();
5455d593ea1361bd7c5ef3353b66d63ac64e95d9d41Elliott Hughes        initNative(pattern);
546adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    }
547adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
548757a7942eed2b0aa457f8517a0259d2ac82c5b18Elliott Hughes    // Used by NumberFormat.getInstance because cloning DecimalFormatSymbols is slow.
549a05602fd81ff398fd6381390a0886608fec5f95cElliott Hughes    DecimalFormat(String pattern, Locale locale) {
550a05602fd81ff398fd6381390a0886608fec5f95cElliott Hughes        this.symbols = new DecimalFormatSymbols(locale);
5515d593ea1361bd7c5ef3353b66d63ac64e95d9d41Elliott Hughes        initNative(pattern);
552f2d5062b67e57ef00ee81fec67480f0d58d66b50Elliott Hughes    }
553a05602fd81ff398fd6381390a0886608fec5f95cElliott Hughes
5545d593ea1361bd7c5ef3353b66d63ac64e95d9d41Elliott Hughes    private void initNative(String pattern) {
555f2d5062b67e57ef00ee81fec67480f0d58d66b50Elliott Hughes        try {
55634a3a0501220dd62b88ddb4a355bad46b15c25a2Elliott Hughes            this.ndf = new NativeDecimalFormat(pattern, symbols);
557f2d5062b67e57ef00ee81fec67480f0d58d66b50Elliott Hughes        } catch (IllegalArgumentException ex) {
558f2d5062b67e57ef00ee81fec67480f0d58d66b50Elliott Hughes            throw new IllegalArgumentException(pattern);
559f2d5062b67e57ef00ee81fec67480f0d58d66b50Elliott Hughes        }
56034a3a0501220dd62b88ddb4a355bad46b15c25a2Elliott Hughes        super.setMaximumFractionDigits(ndf.getMaximumFractionDigits());
56134a3a0501220dd62b88ddb4a355bad46b15c25a2Elliott Hughes        super.setMaximumIntegerDigits(ndf.getMaximumIntegerDigits());
56234a3a0501220dd62b88ddb4a355bad46b15c25a2Elliott Hughes        super.setMinimumFractionDigits(ndf.getMinimumFractionDigits());
56334a3a0501220dd62b88ddb4a355bad46b15c25a2Elliott Hughes        super.setMinimumIntegerDigits(ndf.getMinimumIntegerDigits());
564a05602fd81ff398fd6381390a0886608fec5f95cElliott Hughes    }
565a05602fd81ff398fd6381390a0886608fec5f95cElliott Hughes
566adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    /**
567adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * Changes the pattern of this decimal format to the specified pattern which
568adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * uses localized pattern characters.
569f33eae7e84eb6d3b0f4e86b59605bb3de73009f3Elliott Hughes     *
570adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * @param pattern
571adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     *            the localized pattern.
5729b354e75f2418e54638e02d153216660b67d01b8Jesse Wilson     * @throws IllegalArgumentException
5739b354e75f2418e54638e02d153216660b67d01b8Jesse Wilson     *            if the pattern cannot be parsed.
574adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     */
575adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    public void applyLocalizedPattern(String pattern) {
57634a3a0501220dd62b88ddb4a355bad46b15c25a2Elliott Hughes        ndf.applyLocalizedPattern(pattern);
577adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    }
578adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
579adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    /**
580adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * Changes the pattern of this decimal format to the specified pattern which
581adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * uses non-localized pattern characters.
582f33eae7e84eb6d3b0f4e86b59605bb3de73009f3Elliott Hughes     *
583adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * @param pattern
584adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     *            the non-localized pattern.
5859b354e75f2418e54638e02d153216660b67d01b8Jesse Wilson     * @throws IllegalArgumentException
5869b354e75f2418e54638e02d153216660b67d01b8Jesse Wilson     *            if the pattern cannot be parsed.
587adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     */
588adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    public void applyPattern(String pattern) {
58934a3a0501220dd62b88ddb4a355bad46b15c25a2Elliott Hughes        ndf.applyPattern(pattern);
590adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    }
591adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
592adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    /**
593adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * Returns a new instance of {@code DecimalFormat} with the same pattern and
59434a3a0501220dd62b88ddb4a355bad46b15c25a2Elliott Hughes     * properties.
595adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     */
596adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    @Override
597adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    public Object clone() {
598adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        DecimalFormat clone = (DecimalFormat) super.clone();
59934a3a0501220dd62b88ddb4a355bad46b15c25a2Elliott Hughes        clone.ndf = (NativeDecimalFormat) ndf.clone();
600adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        clone.symbols = (DecimalFormatSymbols) symbols.clone();
601adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        return clone;
602adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    }
603adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
604adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    /**
605adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * Compares the specified object to this decimal format and indicates if
606adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * they are equal. In order to be equal, {@code object} must be an instance
607adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * of {@code DecimalFormat} with the same pattern and properties.
608f33eae7e84eb6d3b0f4e86b59605bb3de73009f3Elliott Hughes     *
609adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * @param object
610adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     *            the object to compare with this object.
611adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * @return {@code true} if the specified object is equal to this decimal
612adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     *         format; {@code false} otherwise.
613adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * @see #hashCode
614adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     */
615adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    @Override
616adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    public boolean equals(Object object) {
617adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        if (this == object) {
618adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project            return true;
619adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        }
620adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        if (!(object instanceof DecimalFormat)) {
621adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project            return false;
622adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        }
623f2d5062b67e57ef00ee81fec67480f0d58d66b50Elliott Hughes        DecimalFormat other = (DecimalFormat) object;
62434a3a0501220dd62b88ddb4a355bad46b15c25a2Elliott Hughes        return (this.ndf == null ? other.ndf == null : this.ndf.equals(other.ndf)) &&
625f2d5062b67e57ef00ee81fec67480f0d58d66b50Elliott Hughes                getDecimalFormatSymbols().equals(other.getDecimalFormatSymbols());
626adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    }
627adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
628adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    /**
629adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * Formats the specified object using the rules of this decimal format and
630adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * returns an {@code AttributedCharacterIterator} with the formatted number
631adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * and attributes.
632f33eae7e84eb6d3b0f4e86b59605bb3de73009f3Elliott Hughes     *
633adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * @param object
634adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     *            the object to format.
635adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * @return an AttributedCharacterIterator with the formatted number and
636adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     *         attributes.
637adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * @throws IllegalArgumentException
638adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     *             if {@code object} cannot be formatted by this format.
639adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * @throws NullPointerException
640adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     *             if {@code object} is {@code null}.
641adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     */
642adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    @Override
643adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    public AttributedCharacterIterator formatToCharacterIterator(Object object) {
644adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        if (object == null) {
645d43b9ef11a1095967a3396b246639b563e1a4128Kenny Root            throw new NullPointerException("object == null");
646adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        }
64734a3a0501220dd62b88ddb4a355bad46b15c25a2Elliott Hughes        return ndf.formatToCharacterIterator(object);
648adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    }
649adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
6503e6f49bbb915b880afe104ec784c66bda55da450Elliott Hughes    private void checkBufferAndFieldPosition(StringBuffer buffer, FieldPosition position) {
6513e6f49bbb915b880afe104ec784c66bda55da450Elliott Hughes        if (buffer == null) {
6523e6f49bbb915b880afe104ec784c66bda55da450Elliott Hughes            throw new NullPointerException("buffer == null");
6533e6f49bbb915b880afe104ec784c66bda55da450Elliott Hughes        }
6543e6f49bbb915b880afe104ec784c66bda55da450Elliott Hughes        if (position == null) {
6553e6f49bbb915b880afe104ec784c66bda55da450Elliott Hughes            throw new NullPointerException("position == null");
6563e6f49bbb915b880afe104ec784c66bda55da450Elliott Hughes        }
6573e6f49bbb915b880afe104ec784c66bda55da450Elliott Hughes    }
6583e6f49bbb915b880afe104ec784c66bda55da450Elliott Hughes
659adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    @Override
660d7e81f7408fb84307d7792b7e3ab8c5a510b294bElliott Hughes    public StringBuffer format(double value, StringBuffer buffer, FieldPosition position) {
6613e6f49bbb915b880afe104ec784c66bda55da450Elliott Hughes        checkBufferAndFieldPosition(buffer, position);
66242db7d138547e3bba75cf6ed02b6a319e30004aeElliott Hughes        // All float/double/Float/Double formatting ends up here...
66342db7d138547e3bba75cf6ed02b6a319e30004aeElliott Hughes        if (roundingMode == RoundingMode.UNNECESSARY) {
66442db7d138547e3bba75cf6ed02b6a319e30004aeElliott Hughes            // ICU4C doesn't support this rounding mode, so we have to fake it.
66542db7d138547e3bba75cf6ed02b6a319e30004aeElliott Hughes            try {
66642db7d138547e3bba75cf6ed02b6a319e30004aeElliott Hughes                setRoundingMode(RoundingMode.UP);
66742db7d138547e3bba75cf6ed02b6a319e30004aeElliott Hughes                String upResult = format(value, new StringBuffer(), new FieldPosition(0)).toString();
66842db7d138547e3bba75cf6ed02b6a319e30004aeElliott Hughes                setRoundingMode(RoundingMode.DOWN);
66942db7d138547e3bba75cf6ed02b6a319e30004aeElliott Hughes                String downResult = format(value, new StringBuffer(), new FieldPosition(0)).toString();
67042db7d138547e3bba75cf6ed02b6a319e30004aeElliott Hughes                if (!upResult.equals(downResult)) {
67142db7d138547e3bba75cf6ed02b6a319e30004aeElliott Hughes                    throw new ArithmeticException("rounding mode UNNECESSARY but rounding required");
67242db7d138547e3bba75cf6ed02b6a319e30004aeElliott Hughes                }
67342db7d138547e3bba75cf6ed02b6a319e30004aeElliott Hughes            } finally {
67442db7d138547e3bba75cf6ed02b6a319e30004aeElliott Hughes                setRoundingMode(RoundingMode.UNNECESSARY);
67542db7d138547e3bba75cf6ed02b6a319e30004aeElliott Hughes            }
67642db7d138547e3bba75cf6ed02b6a319e30004aeElliott Hughes        }
67734a3a0501220dd62b88ddb4a355bad46b15c25a2Elliott Hughes        buffer.append(ndf.formatDouble(value, position));
6783e6f49bbb915b880afe104ec784c66bda55da450Elliott Hughes        return buffer;
679adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    }
680adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
681adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    @Override
682d7e81f7408fb84307d7792b7e3ab8c5a510b294bElliott Hughes    public StringBuffer format(long value, StringBuffer buffer, FieldPosition position) {
6833e6f49bbb915b880afe104ec784c66bda55da450Elliott Hughes        checkBufferAndFieldPosition(buffer, position);
68434a3a0501220dd62b88ddb4a355bad46b15c25a2Elliott Hughes        buffer.append(ndf.formatLong(value, position));
6853e6f49bbb915b880afe104ec784c66bda55da450Elliott Hughes        return buffer;
686adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    }
687adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
688adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    @Override
6893e6f49bbb915b880afe104ec784c66bda55da450Elliott Hughes    public final StringBuffer format(Object number, StringBuffer buffer, FieldPosition position) {
6903e6f49bbb915b880afe104ec784c66bda55da450Elliott Hughes        checkBufferAndFieldPosition(buffer, position);
691d7e81f7408fb84307d7792b7e3ab8c5a510b294bElliott Hughes        if (number instanceof BigInteger) {
692d7e81f7408fb84307d7792b7e3ab8c5a510b294bElliott Hughes            BigInteger bigInteger = (BigInteger) number;
69346cb23aaaba1b0553d02d32713abad97bd4b0428Elliott Hughes            char[] chars = (bigInteger.bitLength() < 64)
69434a3a0501220dd62b88ddb4a355bad46b15c25a2Elliott Hughes                    ? ndf.formatLong(bigInteger.longValue(), position)
69534a3a0501220dd62b88ddb4a355bad46b15c25a2Elliott Hughes                    : ndf.formatBigInteger(bigInteger, position);
69646cb23aaaba1b0553d02d32713abad97bd4b0428Elliott Hughes            buffer.append(chars);
6973e6f49bbb915b880afe104ec784c66bda55da450Elliott Hughes            return buffer;
698d7e81f7408fb84307d7792b7e3ab8c5a510b294bElliott Hughes        } else if (number instanceof BigDecimal) {
69934a3a0501220dd62b88ddb4a355bad46b15c25a2Elliott Hughes            buffer.append(ndf.formatBigDecimal((BigDecimal) number, position));
7003e6f49bbb915b880afe104ec784c66bda55da450Elliott Hughes            return buffer;
701adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        }
7023e6f49bbb915b880afe104ec784c66bda55da450Elliott Hughes        return super.format(number, buffer, position);
703adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    }
704adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
705adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    /**
706adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * Returns the {@code DecimalFormatSymbols} used by this decimal format.
707f33eae7e84eb6d3b0f4e86b59605bb3de73009f3Elliott Hughes     *
708adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * @return a copy of the {@code DecimalFormatSymbols} used by this decimal
709adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     *         format.
710adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     */
711adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    public DecimalFormatSymbols getDecimalFormatSymbols() {
712adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        return (DecimalFormatSymbols) symbols.clone();
713adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    }
714adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
715adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    /**
716adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * Returns the currency used by this decimal format.
717f33eae7e84eb6d3b0f4e86b59605bb3de73009f3Elliott Hughes     *
718adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * @return the currency used by this decimal format.
719adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * @see DecimalFormatSymbols#getCurrency()
720adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     */
721adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    @Override
722adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    public Currency getCurrency() {
7235d593ea1361bd7c5ef3353b66d63ac64e95d9d41Elliott Hughes        return symbols.getCurrency();
724adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    }
725adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
726adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    /**
727adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * Returns the number of digits grouped together by the grouping separator.
728adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * This only allows to get the primary grouping size. There is no API to get
729adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * the secondary grouping size.
7309b354e75f2418e54638e02d153216660b67d01b8Jesse Wilson     *
731adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * @return the number of digits grouped together.
732adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     */
733adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    public int getGroupingSize() {
73434a3a0501220dd62b88ddb4a355bad46b15c25a2Elliott Hughes        return ndf.getGroupingSize();
735adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    }
736adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
737adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    /**
738adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * Returns the multiplier which is applied to the number before formatting
739adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * or after parsing.
740f33eae7e84eb6d3b0f4e86b59605bb3de73009f3Elliott Hughes     *
741adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * @return the multiplier.
742adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     */
743adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    public int getMultiplier() {
74434a3a0501220dd62b88ddb4a355bad46b15c25a2Elliott Hughes        return ndf.getMultiplier();
745adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    }
746adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
747adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    /**
748adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * Returns the prefix which is formatted or parsed before a negative number.
749f33eae7e84eb6d3b0f4e86b59605bb3de73009f3Elliott Hughes     *
750adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * @return the negative prefix.
751adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     */
752adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    public String getNegativePrefix() {
75334a3a0501220dd62b88ddb4a355bad46b15c25a2Elliott Hughes        return ndf.getNegativePrefix();
754adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    }
755adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
756adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    /**
757adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * Returns the suffix which is formatted or parsed after a negative number.
758f33eae7e84eb6d3b0f4e86b59605bb3de73009f3Elliott Hughes     *
759adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * @return the negative suffix.
760adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     */
761adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    public String getNegativeSuffix() {
76234a3a0501220dd62b88ddb4a355bad46b15c25a2Elliott Hughes        return ndf.getNegativeSuffix();
763adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    }
764adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
765adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    /**
766adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * Returns the prefix which is formatted or parsed before a positive number.
767f33eae7e84eb6d3b0f4e86b59605bb3de73009f3Elliott Hughes     *
768adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * @return the positive prefix.
769adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     */
770adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    public String getPositivePrefix() {
77134a3a0501220dd62b88ddb4a355bad46b15c25a2Elliott Hughes        return ndf.getPositivePrefix();
772adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    }
773adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
774adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    /**
775adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * Returns the suffix which is formatted or parsed after a positive number.
776f33eae7e84eb6d3b0f4e86b59605bb3de73009f3Elliott Hughes     *
777adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * @return the positive suffix.
778adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     */
779adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    public String getPositiveSuffix() {
78034a3a0501220dd62b88ddb4a355bad46b15c25a2Elliott Hughes        return ndf.getPositiveSuffix();
781adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    }
782adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
783adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    @Override
784adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    public int hashCode() {
78534a3a0501220dd62b88ddb4a355bad46b15c25a2Elliott Hughes        return getPositivePrefix().hashCode();
786adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    }
787adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
788adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    /**
789adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * Indicates whether the decimal separator is shown when there are no
790adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * fractional digits.
791f33eae7e84eb6d3b0f4e86b59605bb3de73009f3Elliott Hughes     *
792adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * @return {@code true} if the decimal separator should always be formatted;
793adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     *         {@code false} otherwise.
794adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     */
795adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    public boolean isDecimalSeparatorAlwaysShown() {
79634a3a0501220dd62b88ddb4a355bad46b15c25a2Elliott Hughes        return ndf.isDecimalSeparatorAlwaysShown();
797adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    }
798adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
799adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    /**
800adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * This value indicates whether the return object of the parse operation is
801adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * of type {@code BigDecimal}. This value defaults to {@code false}.
802f33eae7e84eb6d3b0f4e86b59605bb3de73009f3Elliott Hughes     *
803adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * @return {@code true} if parse always returns {@code BigDecimals},
804adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     *         {@code false} if the type of the result is {@code Long} or
805adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     *         {@code Double}.
806adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     */
807adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    public boolean isParseBigDecimal() {
80834a3a0501220dd62b88ddb4a355bad46b15c25a2Elliott Hughes        return ndf.isParseBigDecimal();
809adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    }
810adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
811adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    /**
812adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * Sets the flag that indicates whether numbers will be parsed as integers.
813adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * When this decimal format is used for parsing and this value is set to
814adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * {@code true}, then the resulting numbers will be of type
815adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * {@code java.lang.Integer}. Special cases are NaN, positive and negative
816adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * infinity, which are still returned as {@code java.lang.Double}.
817f33eae7e84eb6d3b0f4e86b59605bb3de73009f3Elliott Hughes     *
8189b354e75f2418e54638e02d153216660b67d01b8Jesse Wilson     *
819adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * @param value
820adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     *            {@code true} that the resulting numbers of parse operations
821adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     *            will be of type {@code java.lang.Integer} except for the
822adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     *            special cases described above.
823adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     */
824adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    @Override
825adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    public void setParseIntegerOnly(boolean value) {
826820dd9b89746b3e3005bd5d23176cde1a69048b7Elliott Hughes        // In this implementation, NativeDecimalFormat is wrapped to
8279b354e75f2418e54638e02d153216660b67d01b8Jesse Wilson        // fulfill most of the format and parse feature. And this method is
828820dd9b89746b3e3005bd5d23176cde1a69048b7Elliott Hughes        // delegated to the wrapped instance of NativeDecimalFormat.
82934a3a0501220dd62b88ddb4a355bad46b15c25a2Elliott Hughes        ndf.setParseIntegerOnly(value);
830adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    }
831adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
832adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    /**
833adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * Indicates whether parsing with this decimal format will only
834adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * return numbers of type {@code java.lang.Integer}.
835f33eae7e84eb6d3b0f4e86b59605bb3de73009f3Elliott Hughes     *
836adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * @return {@code true} if this {@code DecimalFormat}'s parse method only
837adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     *         returns {@code java.lang.Integer}; {@code false} otherwise.
838adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     */
839adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    @Override
840adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    public boolean isParseIntegerOnly() {
84134a3a0501220dd62b88ddb4a355bad46b15c25a2Elliott Hughes        return ndf.isParseIntegerOnly();
842adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    }
843adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
844adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    private static final Double NEGATIVE_ZERO_DOUBLE = new Double(-0.0);
845adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
846adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    /**
847adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * Parses a {@code Long} or {@code Double} from the specified string
848adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * starting at the index specified by {@code position}. If the string is
849adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * successfully parsed then the index of the {@code ParsePosition} is
850adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * updated to the index following the parsed text. On error, the index is
851adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * unchanged and the error index of {@code ParsePosition} is set to the
852adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * index where the error occurred.
853f33eae7e84eb6d3b0f4e86b59605bb3de73009f3Elliott Hughes     *
854adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * @param string
855adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     *            the string to parse.
856adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * @param position
857adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     *            input/output parameter, specifies the start index in
858adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     *            {@code string} from where to start parsing. If parsing is
859adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     *            successful, it is updated with the index following the parsed
860adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     *            text; on error, the index is unchanged and the error index is
861adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     *            set to the index where the error occurred.
862adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * @return a {@code Long} or {@code Double} resulting from the parse or
863adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     *         {@code null} if there is an error. The result will be a
864adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     *         {@code Long} if the parsed number is an integer in the range of a
865adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     *         long, otherwise the result is a {@code Double}. If
866adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     *         {@code isParseBigDecimal} is {@code true} then it returns the
867adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     *         result as a {@code BigDecimal}.
868adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     */
869adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    @Override
870adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    public Number parse(String string, ParsePosition position) {
87134a3a0501220dd62b88ddb4a355bad46b15c25a2Elliott Hughes        Number number = ndf.parse(string, position);
872b46dab348e2007bc08abaf7ecae34d89a2474e50Elliott Hughes        if (number == null) {
873adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project            return null;
874adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        }
875adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        if (this.isParseBigDecimal()) {
876adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project            if (number instanceof Long) {
877adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project                return new BigDecimal(number.longValue());
878adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project            }
879adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project            if ((number instanceof Double) && !((Double) number).isInfinite()
880adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project                    && !((Double) number).isNaN()) {
881adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
882adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project                return new BigDecimal(number.toString());
883adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project            }
884adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project            if (number instanceof BigInteger) {
885adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project                return new BigDecimal(number.toString());
886adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project            }
887adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project            return number;
888adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        }
889adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        if ((number instanceof BigDecimal) || (number instanceof BigInteger)) {
890adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project            return new Double(number.doubleValue());
891adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        }
892adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        if (this.isParseIntegerOnly() && number.equals(NEGATIVE_ZERO_DOUBLE)) {
893fb0ec0e650bf8be35acb0d47da0311a7c446aa33Elliott Hughes            return Long.valueOf(0);
894adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        }
895adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        return number;
896adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
897adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    }
898adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
899adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    /**
900adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * Sets the {@code DecimalFormatSymbols} used by this decimal format.
901f33eae7e84eb6d3b0f4e86b59605bb3de73009f3Elliott Hughes     *
902adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * @param value
903adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     *            the {@code DecimalFormatSymbols} to set.
904adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     */
905adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    public void setDecimalFormatSymbols(DecimalFormatSymbols value) {
906adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        if (value != null) {
907fb0ec0e650bf8be35acb0d47da0311a7c446aa33Elliott Hughes            // The Java object is canonical, and we copy down to native code.
908f2d5062b67e57ef00ee81fec67480f0d58d66b50Elliott Hughes            this.symbols = (DecimalFormatSymbols) value.clone();
90934a3a0501220dd62b88ddb4a355bad46b15c25a2Elliott Hughes            ndf.setDecimalFormatSymbols(this.symbols);
910adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        }
911adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    }
912adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
913adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    /**
914adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * Sets the currency used by this decimal format. The min and max fraction
915adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * digits remain the same.
916f33eae7e84eb6d3b0f4e86b59605bb3de73009f3Elliott Hughes     *
917adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * @param currency
918adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     *            the currency this {@code DecimalFormat} should use.
919adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * @see DecimalFormatSymbols#setCurrency(Currency)
920adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     */
921adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    @Override
922adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    public void setCurrency(Currency currency) {
92334a3a0501220dd62b88ddb4a355bad46b15c25a2Elliott Hughes        ndf.setCurrency(Currency.getInstance(currency.getCurrencyCode()));
924adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        symbols.setCurrency(currency);
925adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    }
926adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
927adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    /**
928adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * Sets whether the decimal separator is shown when there are no fractional
929adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * digits.
930f33eae7e84eb6d3b0f4e86b59605bb3de73009f3Elliott Hughes     *
931adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * @param value
932adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     *            {@code true} if the decimal separator should always be
933adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     *            formatted; {@code false} otherwise.
934adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     */
935adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    public void setDecimalSeparatorAlwaysShown(boolean value) {
93634a3a0501220dd62b88ddb4a355bad46b15c25a2Elliott Hughes        ndf.setDecimalSeparatorAlwaysShown(value);
937adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    }
938adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
939adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    /**
940adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * Sets the number of digits grouped together by the grouping separator.
941adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * This only allows to set the primary grouping size; the secondary grouping
942adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * size can only be set with a pattern.
9439b354e75f2418e54638e02d153216660b67d01b8Jesse Wilson     *
944adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * @param value
945adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     *            the number of digits grouped together.
946adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     */
947adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    public void setGroupingSize(int value) {
94834a3a0501220dd62b88ddb4a355bad46b15c25a2Elliott Hughes        ndf.setGroupingSize(value);
949adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    }
950adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
951adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    /**
952adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * Sets whether or not grouping will be used in this format. Grouping
953adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * affects both parsing and formatting.
954f33eae7e84eb6d3b0f4e86b59605bb3de73009f3Elliott Hughes     *
955adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * @param value
956adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     *            {@code true} if grouping is used; {@code false} otherwise.
957adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     */
958adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    @Override
959adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    public void setGroupingUsed(boolean value) {
96034a3a0501220dd62b88ddb4a355bad46b15c25a2Elliott Hughes        ndf.setGroupingUsed(value);
961adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    }
962adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
963adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    /**
964adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * Indicates whether grouping will be used in this format.
965f33eae7e84eb6d3b0f4e86b59605bb3de73009f3Elliott Hughes     *
966adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * @return {@code true} if grouping is used; {@code false} otherwise.
967adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     */
968adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    @Override
969adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    public boolean isGroupingUsed() {
97034a3a0501220dd62b88ddb4a355bad46b15c25a2Elliott Hughes        return ndf.isGroupingUsed();
971adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    }
972adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
973adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    /**
974f2d5062b67e57ef00ee81fec67480f0d58d66b50Elliott Hughes     * Sets the maximum number of digits after the decimal point.
975f2d5062b67e57ef00ee81fec67480f0d58d66b50Elliott Hughes     * If the value passed is negative then it is replaced by 0.
976f2d5062b67e57ef00ee81fec67480f0d58d66b50Elliott Hughes     * Regardless of this setting, no more than 340 digits will be used.
977f33eae7e84eb6d3b0f4e86b59605bb3de73009f3Elliott Hughes     *
978f2d5062b67e57ef00ee81fec67480f0d58d66b50Elliott Hughes     * @param value the maximum number of fraction digits.
979adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     */
980adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    @Override
981adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    public void setMaximumFractionDigits(int value) {
982adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        super.setMaximumFractionDigits(value);
98334a3a0501220dd62b88ddb4a355bad46b15c25a2Elliott Hughes        ndf.setMaximumFractionDigits(getMaximumFractionDigits());
98442db7d138547e3bba75cf6ed02b6a319e30004aeElliott Hughes        // Changing the maximum fraction digits needs to update ICU4C's rounding configuration.
98542db7d138547e3bba75cf6ed02b6a319e30004aeElliott Hughes        setRoundingMode(roundingMode);
986adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    }
987adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
988adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    /**
989f2d5062b67e57ef00ee81fec67480f0d58d66b50Elliott Hughes     * Sets the maximum number of digits before the decimal point.
990f2d5062b67e57ef00ee81fec67480f0d58d66b50Elliott Hughes     * If the value passed is negative then it is replaced by 0.
991f2d5062b67e57ef00ee81fec67480f0d58d66b50Elliott Hughes     * Regardless of this setting, no more than 309 digits will be used.
992f33eae7e84eb6d3b0f4e86b59605bb3de73009f3Elliott Hughes     *
993f2d5062b67e57ef00ee81fec67480f0d58d66b50Elliott Hughes     * @param value the maximum number of integer digits.
994adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     */
995adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    @Override
996adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    public void setMaximumIntegerDigits(int value) {
997adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        super.setMaximumIntegerDigits(value);
99834a3a0501220dd62b88ddb4a355bad46b15c25a2Elliott Hughes        ndf.setMaximumIntegerDigits(getMaximumIntegerDigits());
999adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    }
1000adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
1001adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    /**
1002f2d5062b67e57ef00ee81fec67480f0d58d66b50Elliott Hughes     * Sets the minimum number of digits after the decimal point.
1003f2d5062b67e57ef00ee81fec67480f0d58d66b50Elliott Hughes     * If the value passed is negative then it is replaced by 0.
1004f2d5062b67e57ef00ee81fec67480f0d58d66b50Elliott Hughes     * Regardless of this setting, no more than 340 digits will be used.
1005f33eae7e84eb6d3b0f4e86b59605bb3de73009f3Elliott Hughes     *
1006f2d5062b67e57ef00ee81fec67480f0d58d66b50Elliott Hughes     * @param value the minimum number of fraction digits.
1007adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     */
1008adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    @Override
1009adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    public void setMinimumFractionDigits(int value) {
1010adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        super.setMinimumFractionDigits(value);
101134a3a0501220dd62b88ddb4a355bad46b15c25a2Elliott Hughes        ndf.setMinimumFractionDigits(getMinimumFractionDigits());
1012adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    }
1013adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
1014adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    /**
1015f2d5062b67e57ef00ee81fec67480f0d58d66b50Elliott Hughes     * Sets the minimum number of digits before the decimal point.
1016f2d5062b67e57ef00ee81fec67480f0d58d66b50Elliott Hughes     * If the value passed is negative then it is replaced by 0.
1017f2d5062b67e57ef00ee81fec67480f0d58d66b50Elliott Hughes     * Regardless of this setting, no more than 309 digits will be used.
1018f33eae7e84eb6d3b0f4e86b59605bb3de73009f3Elliott Hughes     *
1019f2d5062b67e57ef00ee81fec67480f0d58d66b50Elliott Hughes     * @param value the minimum number of integer digits.
1020adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     */
1021adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    @Override
1022adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    public void setMinimumIntegerDigits(int value) {
1023adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        super.setMinimumIntegerDigits(value);
102434a3a0501220dd62b88ddb4a355bad46b15c25a2Elliott Hughes        ndf.setMinimumIntegerDigits(getMinimumIntegerDigits());
1025adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    }
1026adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
1027adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    /**
1028adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * Sets the multiplier which is applied to the number before formatting or
1029adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * after parsing.
1030f33eae7e84eb6d3b0f4e86b59605bb3de73009f3Elliott Hughes     *
1031adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * @param value
1032adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     *            the multiplier.
1033adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     */
1034adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    public void setMultiplier(int value) {
103534a3a0501220dd62b88ddb4a355bad46b15c25a2Elliott Hughes        ndf.setMultiplier(value);
1036adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    }
1037adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
1038adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    /**
1039adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * Sets the prefix which is formatted or parsed before a negative number.
1040f33eae7e84eb6d3b0f4e86b59605bb3de73009f3Elliott Hughes     *
1041adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * @param value
1042adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     *            the negative prefix.
1043adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     */
1044adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    public void setNegativePrefix(String value) {
104534a3a0501220dd62b88ddb4a355bad46b15c25a2Elliott Hughes        ndf.setNegativePrefix(value);
1046adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    }
1047adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
1048adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    /**
1049adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * Sets the suffix which is formatted or parsed after a negative number.
1050f33eae7e84eb6d3b0f4e86b59605bb3de73009f3Elliott Hughes     *
1051adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * @param value
1052adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     *            the negative suffix.
1053adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     */
1054adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    public void setNegativeSuffix(String value) {
105534a3a0501220dd62b88ddb4a355bad46b15c25a2Elliott Hughes        ndf.setNegativeSuffix(value);
1056adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    }
1057adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
1058adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    /**
1059adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * Sets the prefix which is formatted or parsed before a positive number.
1060f33eae7e84eb6d3b0f4e86b59605bb3de73009f3Elliott Hughes     *
1061adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * @param value
1062adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     *            the positive prefix.
1063adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     */
1064adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    public void setPositivePrefix(String value) {
106534a3a0501220dd62b88ddb4a355bad46b15c25a2Elliott Hughes        ndf.setPositivePrefix(value);
1066adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    }
1067adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
1068adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    /**
1069adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * Sets the suffix which is formatted or parsed after a positive number.
1070f33eae7e84eb6d3b0f4e86b59605bb3de73009f3Elliott Hughes     *
1071adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * @param value
1072adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     *            the positive suffix.
1073adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     */
1074adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    public void setPositiveSuffix(String value) {
107534a3a0501220dd62b88ddb4a355bad46b15c25a2Elliott Hughes        ndf.setPositiveSuffix(value);
1076adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    }
1077adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
1078adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    /**
10795501a3d4b3d7657c183ed5446fe67fa011fbf70bElliott Hughes     * Sets the behavior of the parse method. If set to {@code true} then all
1080adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * the returned objects will be of type {@code BigDecimal}.
1081f33eae7e84eb6d3b0f4e86b59605bb3de73009f3Elliott Hughes     *
1082adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * @param newValue
1083adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     *            {@code true} if all the returned objects should be of type
1084adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     *            {@code BigDecimal}; {@code false} otherwise.
1085adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     */
1086adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    public void setParseBigDecimal(boolean newValue) {
108734a3a0501220dd62b88ddb4a355bad46b15c25a2Elliott Hughes        ndf.setParseBigDecimal(newValue);
1088adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    }
1089adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
1090adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    /**
1091adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * Returns the pattern of this decimal format using localized pattern
1092adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * characters.
1093f33eae7e84eb6d3b0f4e86b59605bb3de73009f3Elliott Hughes     *
1094adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * @return the localized pattern.
1095adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     */
1096adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    public String toLocalizedPattern() {
109734a3a0501220dd62b88ddb4a355bad46b15c25a2Elliott Hughes        return ndf.toLocalizedPattern();
1098adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    }
1099adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
1100adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    /**
1101adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * Returns the pattern of this decimal format using non-localized pattern
1102adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * characters.
1103f33eae7e84eb6d3b0f4e86b59605bb3de73009f3Elliott Hughes     *
1104adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * @return the non-localized pattern.
1105adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     */
1106adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    public String toPattern() {
110734a3a0501220dd62b88ddb4a355bad46b15c25a2Elliott Hughes        return ndf.toPattern();
1108adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    }
1109adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
1110adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    // the fields list to be serialized
1111adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    private static final ObjectStreamField[] serialPersistentFields = {
111228eb98ecd43c27702e85b0561e040e2da10320a6Elliott Hughes        new ObjectStreamField("positivePrefix", String.class),
111328eb98ecd43c27702e85b0561e040e2da10320a6Elliott Hughes        new ObjectStreamField("positiveSuffix", String.class),
111428eb98ecd43c27702e85b0561e040e2da10320a6Elliott Hughes        new ObjectStreamField("negativePrefix", String.class),
111528eb98ecd43c27702e85b0561e040e2da10320a6Elliott Hughes        new ObjectStreamField("negativeSuffix", String.class),
111628eb98ecd43c27702e85b0561e040e2da10320a6Elliott Hughes        new ObjectStreamField("posPrefixPattern", String.class),
111728eb98ecd43c27702e85b0561e040e2da10320a6Elliott Hughes        new ObjectStreamField("posSuffixPattern", String.class),
111828eb98ecd43c27702e85b0561e040e2da10320a6Elliott Hughes        new ObjectStreamField("negPrefixPattern", String.class),
111928eb98ecd43c27702e85b0561e040e2da10320a6Elliott Hughes        new ObjectStreamField("negSuffixPattern", String.class),
112028eb98ecd43c27702e85b0561e040e2da10320a6Elliott Hughes        new ObjectStreamField("multiplier", int.class),
112128eb98ecd43c27702e85b0561e040e2da10320a6Elliott Hughes        new ObjectStreamField("groupingSize", byte.class),
112228eb98ecd43c27702e85b0561e040e2da10320a6Elliott Hughes        new ObjectStreamField("groupingUsed", boolean.class),
112328eb98ecd43c27702e85b0561e040e2da10320a6Elliott Hughes        new ObjectStreamField("decimalSeparatorAlwaysShown", boolean.class),
112428eb98ecd43c27702e85b0561e040e2da10320a6Elliott Hughes        new ObjectStreamField("parseBigDecimal", boolean.class),
112528eb98ecd43c27702e85b0561e040e2da10320a6Elliott Hughes        new ObjectStreamField("roundingMode", RoundingMode.class),
112628eb98ecd43c27702e85b0561e040e2da10320a6Elliott Hughes        new ObjectStreamField("symbols", DecimalFormatSymbols.class),
112728eb98ecd43c27702e85b0561e040e2da10320a6Elliott Hughes        new ObjectStreamField("useExponentialNotation", boolean.class),
112828eb98ecd43c27702e85b0561e040e2da10320a6Elliott Hughes        new ObjectStreamField("minExponentDigits", byte.class),
112928eb98ecd43c27702e85b0561e040e2da10320a6Elliott Hughes        new ObjectStreamField("maximumIntegerDigits", int.class),
113028eb98ecd43c27702e85b0561e040e2da10320a6Elliott Hughes        new ObjectStreamField("minimumIntegerDigits", int.class),
113128eb98ecd43c27702e85b0561e040e2da10320a6Elliott Hughes        new ObjectStreamField("maximumFractionDigits", int.class),
113228eb98ecd43c27702e85b0561e040e2da10320a6Elliott Hughes        new ObjectStreamField("minimumFractionDigits", int.class),
113328eb98ecd43c27702e85b0561e040e2da10320a6Elliott Hughes        new ObjectStreamField("serialVersionOnStream", int.class),
113428eb98ecd43c27702e85b0561e040e2da10320a6Elliott Hughes    };
1135adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
1136adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    /**
1137adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * Writes serialized fields following serialized forms specified by Java
1138adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * specification.
1139f33eae7e84eb6d3b0f4e86b59605bb3de73009f3Elliott Hughes     *
1140adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * @param stream
1141adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     *            the output stream to write serialized bytes
1142adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * @throws IOException
1143adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     *             if some I/O error occurs
1144adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * @throws ClassNotFoundException
1145adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     */
11461c4b8eb0aebfe7f99c10fb1d01716946e8e74ad7Elliott Hughes    private void writeObject(ObjectOutputStream stream) throws IOException, ClassNotFoundException {
1147adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        ObjectOutputStream.PutField fields = stream.putFields();
114834a3a0501220dd62b88ddb4a355bad46b15c25a2Elliott Hughes        fields.put("positivePrefix", ndf.getPositivePrefix());
114934a3a0501220dd62b88ddb4a355bad46b15c25a2Elliott Hughes        fields.put("positiveSuffix", ndf.getPositiveSuffix());
115034a3a0501220dd62b88ddb4a355bad46b15c25a2Elliott Hughes        fields.put("negativePrefix", ndf.getNegativePrefix());
115134a3a0501220dd62b88ddb4a355bad46b15c25a2Elliott Hughes        fields.put("negativeSuffix", ndf.getNegativeSuffix());
1152186ae3b4b3ccad7baad8a33d59952263427ddda5Elliott Hughes        fields.put("posPrefixPattern", (String) null);
1153186ae3b4b3ccad7baad8a33d59952263427ddda5Elliott Hughes        fields.put("posSuffixPattern", (String) null);
1154186ae3b4b3ccad7baad8a33d59952263427ddda5Elliott Hughes        fields.put("negPrefixPattern", (String) null);
1155186ae3b4b3ccad7baad8a33d59952263427ddda5Elliott Hughes        fields.put("negSuffixPattern", (String) null);
115634a3a0501220dd62b88ddb4a355bad46b15c25a2Elliott Hughes        fields.put("multiplier", ndf.getMultiplier());
115734a3a0501220dd62b88ddb4a355bad46b15c25a2Elliott Hughes        fields.put("groupingSize", (byte) ndf.getGroupingSize());
115834a3a0501220dd62b88ddb4a355bad46b15c25a2Elliott Hughes        fields.put("groupingUsed", ndf.isGroupingUsed());
115934a3a0501220dd62b88ddb4a355bad46b15c25a2Elliott Hughes        fields.put("decimalSeparatorAlwaysShown", ndf.isDecimalSeparatorAlwaysShown());
116034a3a0501220dd62b88ddb4a355bad46b15c25a2Elliott Hughes        fields.put("parseBigDecimal", ndf.isParseBigDecimal());
116142db7d138547e3bba75cf6ed02b6a319e30004aeElliott Hughes        fields.put("roundingMode", roundingMode);
11629b354e75f2418e54638e02d153216660b67d01b8Jesse Wilson        fields.put("symbols", symbols);
1163f2d5062b67e57ef00ee81fec67480f0d58d66b50Elliott Hughes        fields.put("useExponentialNotation", false);
1164f8ba29b25f7fc2ef93ab39a8ac436c94d55b3984Elliott Hughes        fields.put("minExponentDigits", (byte) 0);
116534a3a0501220dd62b88ddb4a355bad46b15c25a2Elliott Hughes        fields.put("maximumIntegerDigits", ndf.getMaximumIntegerDigits());
116634a3a0501220dd62b88ddb4a355bad46b15c25a2Elliott Hughes        fields.put("minimumIntegerDigits", ndf.getMinimumIntegerDigits());
116734a3a0501220dd62b88ddb4a355bad46b15c25a2Elliott Hughes        fields.put("maximumFractionDigits", ndf.getMaximumFractionDigits());
116834a3a0501220dd62b88ddb4a355bad46b15c25a2Elliott Hughes        fields.put("minimumFractionDigits", ndf.getMinimumFractionDigits());
116942db7d138547e3bba75cf6ed02b6a319e30004aeElliott Hughes        fields.put("serialVersionOnStream", 4);
1170adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        stream.writeFields();
1171adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    }
1172adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
1173adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    /**
1174adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * Reads serialized fields following serialized forms specified by Java
1175adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * specification.
1176f33eae7e84eb6d3b0f4e86b59605bb3de73009f3Elliott Hughes     *
1177adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * @param stream
1178adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     *            the input stream to read serialized bytes
1179adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * @throws IOException
1180adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     *             if some I/O error occurs
1181adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * @throws ClassNotFoundException
1182adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     *             if some class of serialized objects or fields cannot be found
1183adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     */
118442db7d138547e3bba75cf6ed02b6a319e30004aeElliott Hughes    private void readObject(ObjectInputStream stream) throws IOException, ClassNotFoundException {
1185186ae3b4b3ccad7baad8a33d59952263427ddda5Elliott Hughes        ObjectInputStream.GetField fields = stream.readFields();
1186186ae3b4b3ccad7baad8a33d59952263427ddda5Elliott Hughes        this.symbols = (DecimalFormatSymbols) fields.get("symbols", null);
1187186ae3b4b3ccad7baad8a33d59952263427ddda5Elliott Hughes
11885d593ea1361bd7c5ef3353b66d63ac64e95d9d41Elliott Hughes        initNative("");
118934a3a0501220dd62b88ddb4a355bad46b15c25a2Elliott Hughes        ndf.setPositivePrefix((String) fields.get("positivePrefix", ""));
119034a3a0501220dd62b88ddb4a355bad46b15c25a2Elliott Hughes        ndf.setPositiveSuffix((String) fields.get("positiveSuffix", ""));
119134a3a0501220dd62b88ddb4a355bad46b15c25a2Elliott Hughes        ndf.setNegativePrefix((String) fields.get("negativePrefix", "-"));
119234a3a0501220dd62b88ddb4a355bad46b15c25a2Elliott Hughes        ndf.setNegativeSuffix((String) fields.get("negativeSuffix", ""));
119334a3a0501220dd62b88ddb4a355bad46b15c25a2Elliott Hughes        ndf.setMultiplier(fields.get("multiplier", 1));
119434a3a0501220dd62b88ddb4a355bad46b15c25a2Elliott Hughes        ndf.setGroupingSize(fields.get("groupingSize", (byte) 3));
119534a3a0501220dd62b88ddb4a355bad46b15c25a2Elliott Hughes        ndf.setGroupingUsed(fields.get("groupingUsed", true));
119634a3a0501220dd62b88ddb4a355bad46b15c25a2Elliott Hughes        ndf.setDecimalSeparatorAlwaysShown(fields.get("decimalSeparatorAlwaysShown", false));
1197186ae3b4b3ccad7baad8a33d59952263427ddda5Elliott Hughes
119842db7d138547e3bba75cf6ed02b6a319e30004aeElliott Hughes        setRoundingMode((RoundingMode) fields.get("roundingMode", RoundingMode.HALF_EVEN));
119942db7d138547e3bba75cf6ed02b6a319e30004aeElliott Hughes
1200186ae3b4b3ccad7baad8a33d59952263427ddda5Elliott Hughes        final int maximumIntegerDigits = fields.get("maximumIntegerDigits", 309);
1201186ae3b4b3ccad7baad8a33d59952263427ddda5Elliott Hughes        final int minimumIntegerDigits = fields.get("minimumIntegerDigits", 309);
1202186ae3b4b3ccad7baad8a33d59952263427ddda5Elliott Hughes        final int maximumFractionDigits = fields.get("maximumFractionDigits", 340);
1203186ae3b4b3ccad7baad8a33d59952263427ddda5Elliott Hughes        final int minimumFractionDigits = fields.get("minimumFractionDigits", 340);
1204fb0ec0e650bf8be35acb0d47da0311a7c446aa33Elliott Hughes        // Tell ICU what we want, then ask it what we can have, and then
1205f2d5062b67e57ef00ee81fec67480f0d58d66b50Elliott Hughes        // set that in our Java object. This isn't RI-compatible, but then very little of our
1206f2d5062b67e57ef00ee81fec67480f0d58d66b50Elliott Hughes        // behavior in this area is, and it's not obvious how we can second-guess ICU (or tell
1207f2d5062b67e57ef00ee81fec67480f0d58d66b50Elliott Hughes        // it to just do exactly what we ask). We only need to do this with maximumIntegerDigits
1208f2d5062b67e57ef00ee81fec67480f0d58d66b50Elliott Hughes        // because ICU doesn't seem to have its own ideas about the other options.
120934a3a0501220dd62b88ddb4a355bad46b15c25a2Elliott Hughes        ndf.setMaximumIntegerDigits(maximumIntegerDigits);
121034a3a0501220dd62b88ddb4a355bad46b15c25a2Elliott Hughes        super.setMaximumIntegerDigits(ndf.getMaximumIntegerDigits());
1211186ae3b4b3ccad7baad8a33d59952263427ddda5Elliott Hughes
1212186ae3b4b3ccad7baad8a33d59952263427ddda5Elliott Hughes        setMinimumIntegerDigits(minimumIntegerDigits);
1213f2d5062b67e57ef00ee81fec67480f0d58d66b50Elliott Hughes        setMinimumFractionDigits(minimumFractionDigits);
1214f2d5062b67e57ef00ee81fec67480f0d58d66b50Elliott Hughes        setMaximumFractionDigits(maximumFractionDigits);
1215186ae3b4b3ccad7baad8a33d59952263427ddda5Elliott Hughes        setParseBigDecimal(fields.get("parseBigDecimal", false));
1216adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
1217186ae3b4b3ccad7baad8a33d59952263427ddda5Elliott Hughes        if (fields.get("serialVersionOnStream", 0) < 3) {
12189b354e75f2418e54638e02d153216660b67d01b8Jesse Wilson            setMaximumIntegerDigits(super.getMaximumIntegerDigits());
1219adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project            setMinimumIntegerDigits(super.getMinimumIntegerDigits());
1220adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project            setMaximumFractionDigits(super.getMaximumFractionDigits());
1221adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project            setMinimumFractionDigits(super.getMinimumFractionDigits());
1222adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        }
1223adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    }
122442db7d138547e3bba75cf6ed02b6a319e30004aeElliott Hughes
122542db7d138547e3bba75cf6ed02b6a319e30004aeElliott Hughes    /**
122642db7d138547e3bba75cf6ed02b6a319e30004aeElliott Hughes     * Returns the {@code RoundingMode} used by this {@code NumberFormat}.
122742db7d138547e3bba75cf6ed02b6a319e30004aeElliott Hughes     * @since 1.6
122842db7d138547e3bba75cf6ed02b6a319e30004aeElliott Hughes     */
122942db7d138547e3bba75cf6ed02b6a319e30004aeElliott Hughes    public RoundingMode getRoundingMode() {
123042db7d138547e3bba75cf6ed02b6a319e30004aeElliott Hughes        return roundingMode;
123142db7d138547e3bba75cf6ed02b6a319e30004aeElliott Hughes    }
123242db7d138547e3bba75cf6ed02b6a319e30004aeElliott Hughes
123342db7d138547e3bba75cf6ed02b6a319e30004aeElliott Hughes    /**
123442db7d138547e3bba75cf6ed02b6a319e30004aeElliott Hughes     * Sets the {@code RoundingMode} used by this {@code NumberFormat}.
123542db7d138547e3bba75cf6ed02b6a319e30004aeElliott Hughes     * @since 1.6
123642db7d138547e3bba75cf6ed02b6a319e30004aeElliott Hughes     */
123742db7d138547e3bba75cf6ed02b6a319e30004aeElliott Hughes    public void setRoundingMode(RoundingMode roundingMode) {
123842db7d138547e3bba75cf6ed02b6a319e30004aeElliott Hughes        if (roundingMode == null) {
1239d43b9ef11a1095967a3396b246639b563e1a4128Kenny Root            throw new NullPointerException("roundingMode == null");
124042db7d138547e3bba75cf6ed02b6a319e30004aeElliott Hughes        }
124142db7d138547e3bba75cf6ed02b6a319e30004aeElliott Hughes        this.roundingMode = roundingMode;
124242db7d138547e3bba75cf6ed02b6a319e30004aeElliott Hughes        if (roundingMode != RoundingMode.UNNECESSARY) { // ICU4C doesn't support UNNECESSARY.
124342db7d138547e3bba75cf6ed02b6a319e30004aeElliott Hughes            double roundingIncrement = 1.0 / Math.pow(10, Math.max(0, getMaximumFractionDigits()));
124434a3a0501220dd62b88ddb4a355bad46b15c25a2Elliott Hughes            ndf.setRoundingMode(roundingMode, roundingIncrement);
124542db7d138547e3bba75cf6ed02b6a319e30004aeElliott Hughes        }
124642db7d138547e3bba75cf6ed02b6a319e30004aeElliott Hughes    }
1247adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project}
1248