jtulach@1334
|
1 |
/*
|
jtulach@1334
|
2 |
* Copyright (c) 1996, 2010, Oracle and/or its affiliates. All rights reserved.
|
jtulach@1334
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
jtulach@1334
|
4 |
*
|
jtulach@1334
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
jtulach@1334
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
jtulach@1334
|
7 |
* published by the Free Software Foundation. Oracle designates this
|
jtulach@1334
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
jtulach@1334
|
9 |
* by Oracle in the LICENSE file that accompanied this code.
|
jtulach@1334
|
10 |
*
|
jtulach@1334
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
jtulach@1334
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
jtulach@1334
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
jtulach@1334
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that
|
jtulach@1334
|
15 |
* accompanied this code).
|
jtulach@1334
|
16 |
*
|
jtulach@1334
|
17 |
* You should have received a copy of the GNU General Public License version
|
jtulach@1334
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
jtulach@1334
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
jtulach@1334
|
20 |
*
|
jtulach@1334
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
jtulach@1334
|
22 |
* or visit www.oracle.com if you need additional information or have any
|
jtulach@1334
|
23 |
* questions.
|
jtulach@1334
|
24 |
*/
|
jtulach@1334
|
25 |
|
jtulach@1334
|
26 |
/*
|
jtulach@1334
|
27 |
* (C) Copyright Taligent, Inc. 1996, 1997 - All Rights Reserved
|
jtulach@1334
|
28 |
* (C) Copyright IBM Corp. 1996 - 1998 - All Rights Reserved
|
jtulach@1334
|
29 |
*
|
jtulach@1334
|
30 |
* The original version of this source code and documentation is copyrighted
|
jtulach@1334
|
31 |
* and owned by Taligent, Inc., a wholly-owned subsidiary of IBM. These
|
jtulach@1334
|
32 |
* materials are provided under terms of a License Agreement between Taligent
|
jtulach@1334
|
33 |
* and Sun. This technology is protected by multiple US and International
|
jtulach@1334
|
34 |
* patents. This notice and attribution to Taligent may not be removed.
|
jtulach@1334
|
35 |
* Taligent is a registered trademark of Taligent, Inc.
|
jtulach@1334
|
36 |
*
|
jtulach@1334
|
37 |
*/
|
jtulach@1334
|
38 |
|
jtulach@1334
|
39 |
package java.text;
|
jtulach@1334
|
40 |
|
jtulach@1334
|
41 |
import java.io.InvalidObjectException;
|
jtulach@1334
|
42 |
import java.io.IOException;
|
jtulach@1334
|
43 |
import java.io.ObjectInputStream;
|
jtulach@1334
|
44 |
import java.math.BigDecimal;
|
jtulach@1334
|
45 |
import java.math.BigInteger;
|
jtulach@1334
|
46 |
import java.math.RoundingMode;
|
jtulach@1334
|
47 |
import java.util.ArrayList;
|
jtulach@1334
|
48 |
import java.util.Currency;
|
jtulach@1334
|
49 |
import java.util.Locale;
|
jtulach@1334
|
50 |
import java.util.ResourceBundle;
|
jtulach@1334
|
51 |
import java.util.concurrent.ConcurrentHashMap;
|
jtulach@1334
|
52 |
import java.util.concurrent.ConcurrentMap;
|
jtulach@1334
|
53 |
import java.util.concurrent.atomic.AtomicInteger;
|
jtulach@1334
|
54 |
import java.util.concurrent.atomic.AtomicLong;
|
jtulach@1334
|
55 |
|
jtulach@1334
|
56 |
/**
|
jtulach@1334
|
57 |
* <code>DecimalFormat</code> is a concrete subclass of
|
jtulach@1334
|
58 |
* <code>NumberFormat</code> that formats decimal numbers. It has a variety of
|
jtulach@1334
|
59 |
* features designed to make it possible to parse and format numbers in any
|
jtulach@1334
|
60 |
* locale, including support for Western, Arabic, and Indic digits. It also
|
jtulach@1334
|
61 |
* supports different kinds of numbers, including integers (123), fixed-point
|
jtulach@1334
|
62 |
* numbers (123.4), scientific notation (1.23E4), percentages (12%), and
|
jtulach@1334
|
63 |
* currency amounts ($123). All of these can be localized.
|
jtulach@1334
|
64 |
*
|
jtulach@1334
|
65 |
* <p>To obtain a <code>NumberFormat</code> for a specific locale, including the
|
jtulach@1334
|
66 |
* default locale, call one of <code>NumberFormat</code>'s factory methods, such
|
jtulach@1334
|
67 |
* as <code>getInstance()</code>. In general, do not call the
|
jtulach@1334
|
68 |
* <code>DecimalFormat</code> constructors directly, since the
|
jtulach@1334
|
69 |
* <code>NumberFormat</code> factory methods may return subclasses other than
|
jtulach@1334
|
70 |
* <code>DecimalFormat</code>. If you need to customize the format object, do
|
jtulach@1334
|
71 |
* something like this:
|
jtulach@1334
|
72 |
*
|
jtulach@1334
|
73 |
* <blockquote><pre>
|
jtulach@1334
|
74 |
* NumberFormat f = NumberFormat.getInstance(loc);
|
jtulach@1334
|
75 |
* if (f instanceof DecimalFormat) {
|
jtulach@1334
|
76 |
* ((DecimalFormat) f).setDecimalSeparatorAlwaysShown(true);
|
jtulach@1334
|
77 |
* }
|
jtulach@1334
|
78 |
* </pre></blockquote>
|
jtulach@1334
|
79 |
*
|
jtulach@1334
|
80 |
* <p>A <code>DecimalFormat</code> comprises a <em>pattern</em> and a set of
|
jtulach@1334
|
81 |
* <em>symbols</em>. The pattern may be set directly using
|
jtulach@1334
|
82 |
* <code>applyPattern()</code>, or indirectly using the API methods. The
|
jtulach@1334
|
83 |
* symbols are stored in a <code>DecimalFormatSymbols</code> object. When using
|
jtulach@1334
|
84 |
* the <code>NumberFormat</code> factory methods, the pattern and symbols are
|
jtulach@1334
|
85 |
* read from localized <code>ResourceBundle</code>s.
|
jtulach@1334
|
86 |
*
|
jtulach@1334
|
87 |
* <h4>Patterns</h4>
|
jtulach@1334
|
88 |
*
|
jtulach@1334
|
89 |
* <code>DecimalFormat</code> patterns have the following syntax:
|
jtulach@1334
|
90 |
* <blockquote><pre>
|
jtulach@1334
|
91 |
* <i>Pattern:</i>
|
jtulach@1334
|
92 |
* <i>PositivePattern</i>
|
jtulach@1334
|
93 |
* <i>PositivePattern</i> ; <i>NegativePattern</i>
|
jtulach@1334
|
94 |
* <i>PositivePattern:</i>
|
jtulach@1334
|
95 |
* <i>Prefix<sub>opt</sub></i> <i>Number</i> <i>Suffix<sub>opt</sub></i>
|
jtulach@1334
|
96 |
* <i>NegativePattern:</i>
|
jtulach@1334
|
97 |
* <i>Prefix<sub>opt</sub></i> <i>Number</i> <i>Suffix<sub>opt</sub></i>
|
jtulach@1334
|
98 |
* <i>Prefix:</i>
|
jtulach@1334
|
99 |
* any Unicode characters except \uFFFE, \uFFFF, and special characters
|
jtulach@1334
|
100 |
* <i>Suffix:</i>
|
jtulach@1334
|
101 |
* any Unicode characters except \uFFFE, \uFFFF, and special characters
|
jtulach@1334
|
102 |
* <i>Number:</i>
|
jtulach@1334
|
103 |
* <i>Integer</i> <i>Exponent<sub>opt</sub></i>
|
jtulach@1334
|
104 |
* <i>Integer</i> . <i>Fraction</i> <i>Exponent<sub>opt</sub></i>
|
jtulach@1334
|
105 |
* <i>Integer:</i>
|
jtulach@1334
|
106 |
* <i>MinimumInteger</i>
|
jtulach@1334
|
107 |
* #
|
jtulach@1334
|
108 |
* # <i>Integer</i>
|
jtulach@1334
|
109 |
* # , <i>Integer</i>
|
jtulach@1334
|
110 |
* <i>MinimumInteger:</i>
|
jtulach@1334
|
111 |
* 0
|
jtulach@1334
|
112 |
* 0 <i>MinimumInteger</i>
|
jtulach@1334
|
113 |
* 0 , <i>MinimumInteger</i>
|
jtulach@1334
|
114 |
* <i>Fraction:</i>
|
jtulach@1334
|
115 |
* <i>MinimumFraction<sub>opt</sub></i> <i>OptionalFraction<sub>opt</sub></i>
|
jtulach@1334
|
116 |
* <i>MinimumFraction:</i>
|
jtulach@1334
|
117 |
* 0 <i>MinimumFraction<sub>opt</sub></i>
|
jtulach@1334
|
118 |
* <i>OptionalFraction:</i>
|
jtulach@1334
|
119 |
* # <i>OptionalFraction<sub>opt</sub></i>
|
jtulach@1334
|
120 |
* <i>Exponent:</i>
|
jtulach@1334
|
121 |
* E <i>MinimumExponent</i>
|
jtulach@1334
|
122 |
* <i>MinimumExponent:</i>
|
jtulach@1334
|
123 |
* 0 <i>MinimumExponent<sub>opt</sub></i>
|
jtulach@1334
|
124 |
* </pre></blockquote>
|
jtulach@1334
|
125 |
*
|
jtulach@1334
|
126 |
* <p>A <code>DecimalFormat</code> pattern contains a positive and negative
|
jtulach@1334
|
127 |
* subpattern, for example, <code>"#,##0.00;(#,##0.00)"</code>. Each
|
jtulach@1334
|
128 |
* subpattern has a prefix, numeric part, and suffix. The negative subpattern
|
jtulach@1334
|
129 |
* is optional; if absent, then the positive subpattern prefixed with the
|
jtulach@1334
|
130 |
* localized minus sign (<code>'-'</code> in most locales) is used as the
|
jtulach@1334
|
131 |
* negative subpattern. That is, <code>"0.00"</code> alone is equivalent to
|
jtulach@1334
|
132 |
* <code>"0.00;-0.00"</code>. If there is an explicit negative subpattern, it
|
jtulach@1334
|
133 |
* serves only to specify the negative prefix and suffix; the number of digits,
|
jtulach@1334
|
134 |
* minimal digits, and other characteristics are all the same as the positive
|
jtulach@1334
|
135 |
* pattern. That means that <code>"#,##0.0#;(#)"</code> produces precisely
|
jtulach@1334
|
136 |
* the same behavior as <code>"#,##0.0#;(#,##0.0#)"</code>.
|
jtulach@1334
|
137 |
*
|
jtulach@1334
|
138 |
* <p>The prefixes, suffixes, and various symbols used for infinity, digits,
|
jtulach@1334
|
139 |
* thousands separators, decimal separators, etc. may be set to arbitrary
|
jtulach@1334
|
140 |
* values, and they will appear properly during formatting. However, care must
|
jtulach@1334
|
141 |
* be taken that the symbols and strings do not conflict, or parsing will be
|
jtulach@1334
|
142 |
* unreliable. For example, either the positive and negative prefixes or the
|
jtulach@1334
|
143 |
* suffixes must be distinct for <code>DecimalFormat.parse()</code> to be able
|
jtulach@1334
|
144 |
* to distinguish positive from negative values. (If they are identical, then
|
jtulach@1334
|
145 |
* <code>DecimalFormat</code> will behave as if no negative subpattern was
|
jtulach@1334
|
146 |
* specified.) Another example is that the decimal separator and thousands
|
jtulach@1334
|
147 |
* separator should be distinct characters, or parsing will be impossible.
|
jtulach@1334
|
148 |
*
|
jtulach@1334
|
149 |
* <p>The grouping separator is commonly used for thousands, but in some
|
jtulach@1334
|
150 |
* countries it separates ten-thousands. The grouping size is a constant number
|
jtulach@1334
|
151 |
* of digits between the grouping characters, such as 3 for 100,000,000 or 4 for
|
jtulach@1334
|
152 |
* 1,0000,0000. If you supply a pattern with multiple grouping characters, the
|
jtulach@1334
|
153 |
* interval between the last one and the end of the integer is the one that is
|
jtulach@1334
|
154 |
* used. So <code>"#,##,###,####"</code> == <code>"######,####"</code> ==
|
jtulach@1334
|
155 |
* <code>"##,####,####"</code>.
|
jtulach@1334
|
156 |
*
|
jtulach@1334
|
157 |
* <h4>Special Pattern Characters</h4>
|
jtulach@1334
|
158 |
*
|
jtulach@1334
|
159 |
* <p>Many characters in a pattern are taken literally; they are matched during
|
jtulach@1334
|
160 |
* parsing and output unchanged during formatting. Special characters, on the
|
jtulach@1334
|
161 |
* other hand, stand for other characters, strings, or classes of characters.
|
jtulach@1334
|
162 |
* They must be quoted, unless noted otherwise, if they are to appear in the
|
jtulach@1334
|
163 |
* prefix or suffix as literals.
|
jtulach@1334
|
164 |
*
|
jtulach@1334
|
165 |
* <p>The characters listed here are used in non-localized patterns. Localized
|
jtulach@1334
|
166 |
* patterns use the corresponding characters taken from this formatter's
|
jtulach@1334
|
167 |
* <code>DecimalFormatSymbols</code> object instead, and these characters lose
|
jtulach@1334
|
168 |
* their special status. Two exceptions are the currency sign and quote, which
|
jtulach@1334
|
169 |
* are not localized.
|
jtulach@1334
|
170 |
*
|
jtulach@1334
|
171 |
* <blockquote>
|
jtulach@1334
|
172 |
* <table border=0 cellspacing=3 cellpadding=0 summary="Chart showing symbol,
|
jtulach@1334
|
173 |
* location, localized, and meaning.">
|
jtulach@1334
|
174 |
* <tr bgcolor="#ccccff">
|
jtulach@1334
|
175 |
* <th align=left>Symbol
|
jtulach@1334
|
176 |
* <th align=left>Location
|
jtulach@1334
|
177 |
* <th align=left>Localized?
|
jtulach@1334
|
178 |
* <th align=left>Meaning
|
jtulach@1334
|
179 |
* <tr valign=top>
|
jtulach@1334
|
180 |
* <td><code>0</code>
|
jtulach@1334
|
181 |
* <td>Number
|
jtulach@1334
|
182 |
* <td>Yes
|
jtulach@1334
|
183 |
* <td>Digit
|
jtulach@1334
|
184 |
* <tr valign=top bgcolor="#eeeeff">
|
jtulach@1334
|
185 |
* <td><code>#</code>
|
jtulach@1334
|
186 |
* <td>Number
|
jtulach@1334
|
187 |
* <td>Yes
|
jtulach@1334
|
188 |
* <td>Digit, zero shows as absent
|
jtulach@1334
|
189 |
* <tr valign=top>
|
jtulach@1334
|
190 |
* <td><code>.</code>
|
jtulach@1334
|
191 |
* <td>Number
|
jtulach@1334
|
192 |
* <td>Yes
|
jtulach@1334
|
193 |
* <td>Decimal separator or monetary decimal separator
|
jtulach@1334
|
194 |
* <tr valign=top bgcolor="#eeeeff">
|
jtulach@1334
|
195 |
* <td><code>-</code>
|
jtulach@1334
|
196 |
* <td>Number
|
jtulach@1334
|
197 |
* <td>Yes
|
jtulach@1334
|
198 |
* <td>Minus sign
|
jtulach@1334
|
199 |
* <tr valign=top>
|
jtulach@1334
|
200 |
* <td><code>,</code>
|
jtulach@1334
|
201 |
* <td>Number
|
jtulach@1334
|
202 |
* <td>Yes
|
jtulach@1334
|
203 |
* <td>Grouping separator
|
jtulach@1334
|
204 |
* <tr valign=top bgcolor="#eeeeff">
|
jtulach@1334
|
205 |
* <td><code>E</code>
|
jtulach@1334
|
206 |
* <td>Number
|
jtulach@1334
|
207 |
* <td>Yes
|
jtulach@1334
|
208 |
* <td>Separates mantissa and exponent in scientific notation.
|
jtulach@1334
|
209 |
* <em>Need not be quoted in prefix or suffix.</em>
|
jtulach@1334
|
210 |
* <tr valign=top>
|
jtulach@1334
|
211 |
* <td><code>;</code>
|
jtulach@1334
|
212 |
* <td>Subpattern boundary
|
jtulach@1334
|
213 |
* <td>Yes
|
jtulach@1334
|
214 |
* <td>Separates positive and negative subpatterns
|
jtulach@1334
|
215 |
* <tr valign=top bgcolor="#eeeeff">
|
jtulach@1334
|
216 |
* <td><code>%</code>
|
jtulach@1334
|
217 |
* <td>Prefix or suffix
|
jtulach@1334
|
218 |
* <td>Yes
|
jtulach@1334
|
219 |
* <td>Multiply by 100 and show as percentage
|
jtulach@1334
|
220 |
* <tr valign=top>
|
jtulach@1334
|
221 |
* <td><code>\u2030</code>
|
jtulach@1334
|
222 |
* <td>Prefix or suffix
|
jtulach@1334
|
223 |
* <td>Yes
|
jtulach@1334
|
224 |
* <td>Multiply by 1000 and show as per mille value
|
jtulach@1334
|
225 |
* <tr valign=top bgcolor="#eeeeff">
|
jtulach@1334
|
226 |
* <td><code>¤</code> (<code>\u00A4</code>)
|
jtulach@1334
|
227 |
* <td>Prefix or suffix
|
jtulach@1334
|
228 |
* <td>No
|
jtulach@1334
|
229 |
* <td>Currency sign, replaced by currency symbol. If
|
jtulach@1334
|
230 |
* doubled, replaced by international currency symbol.
|
jtulach@1334
|
231 |
* If present in a pattern, the monetary decimal separator
|
jtulach@1334
|
232 |
* is used instead of the decimal separator.
|
jtulach@1334
|
233 |
* <tr valign=top>
|
jtulach@1334
|
234 |
* <td><code>'</code>
|
jtulach@1334
|
235 |
* <td>Prefix or suffix
|
jtulach@1334
|
236 |
* <td>No
|
jtulach@1334
|
237 |
* <td>Used to quote special characters in a prefix or suffix,
|
jtulach@1334
|
238 |
* for example, <code>"'#'#"</code> formats 123 to
|
jtulach@1334
|
239 |
* <code>"#123"</code>. To create a single quote
|
jtulach@1334
|
240 |
* itself, use two in a row: <code>"# o''clock"</code>.
|
jtulach@1334
|
241 |
* </table>
|
jtulach@1334
|
242 |
* </blockquote>
|
jtulach@1334
|
243 |
*
|
jtulach@1334
|
244 |
* <h4>Scientific Notation</h4>
|
jtulach@1334
|
245 |
*
|
jtulach@1334
|
246 |
* <p>Numbers in scientific notation are expressed as the product of a mantissa
|
jtulach@1334
|
247 |
* and a power of ten, for example, 1234 can be expressed as 1.234 x 10^3. The
|
jtulach@1334
|
248 |
* mantissa is often in the range 1.0 <= x < 10.0, but it need not be.
|
jtulach@1334
|
249 |
* <code>DecimalFormat</code> can be instructed to format and parse scientific
|
jtulach@1334
|
250 |
* notation <em>only via a pattern</em>; there is currently no factory method
|
jtulach@1334
|
251 |
* that creates a scientific notation format. In a pattern, the exponent
|
jtulach@1334
|
252 |
* character immediately followed by one or more digit characters indicates
|
jtulach@1334
|
253 |
* scientific notation. Example: <code>"0.###E0"</code> formats the number
|
jtulach@1334
|
254 |
* 1234 as <code>"1.234E3"</code>.
|
jtulach@1334
|
255 |
*
|
jtulach@1334
|
256 |
* <ul>
|
jtulach@1334
|
257 |
* <li>The number of digit characters after the exponent character gives the
|
jtulach@1334
|
258 |
* minimum exponent digit count. There is no maximum. Negative exponents are
|
jtulach@1334
|
259 |
* formatted using the localized minus sign, <em>not</em> the prefix and suffix
|
jtulach@1334
|
260 |
* from the pattern. This allows patterns such as <code>"0.###E0 m/s"</code>.
|
jtulach@1334
|
261 |
*
|
jtulach@1334
|
262 |
* <li>The minimum and maximum number of integer digits are interpreted
|
jtulach@1334
|
263 |
* together:
|
jtulach@1334
|
264 |
*
|
jtulach@1334
|
265 |
* <ul>
|
jtulach@1334
|
266 |
* <li>If the maximum number of integer digits is greater than their minimum number
|
jtulach@1334
|
267 |
* and greater than 1, it forces the exponent to be a multiple of the maximum
|
jtulach@1334
|
268 |
* number of integer digits, and the minimum number of integer digits to be
|
jtulach@1334
|
269 |
* interpreted as 1. The most common use of this is to generate
|
jtulach@1334
|
270 |
* <em>engineering notation</em>, in which the exponent is a multiple of three,
|
jtulach@1334
|
271 |
* e.g., <code>"##0.#####E0"</code>. Using this pattern, the number 12345
|
jtulach@1334
|
272 |
* formats to <code>"12.345E3"</code>, and 123456 formats to
|
jtulach@1334
|
273 |
* <code>"123.456E3"</code>.
|
jtulach@1334
|
274 |
*
|
jtulach@1334
|
275 |
* <li>Otherwise, the minimum number of integer digits is achieved by adjusting the
|
jtulach@1334
|
276 |
* exponent. Example: 0.00123 formatted with <code>"00.###E0"</code> yields
|
jtulach@1334
|
277 |
* <code>"12.3E-4"</code>.
|
jtulach@1334
|
278 |
* </ul>
|
jtulach@1334
|
279 |
*
|
jtulach@1334
|
280 |
* <li>The number of significant digits in the mantissa is the sum of the
|
jtulach@1334
|
281 |
* <em>minimum integer</em> and <em>maximum fraction</em> digits, and is
|
jtulach@1334
|
282 |
* unaffected by the maximum integer digits. For example, 12345 formatted with
|
jtulach@1334
|
283 |
* <code>"##0.##E0"</code> is <code>"12.3E3"</code>. To show all digits, set
|
jtulach@1334
|
284 |
* the significant digits count to zero. The number of significant digits
|
jtulach@1334
|
285 |
* does not affect parsing.
|
jtulach@1334
|
286 |
*
|
jtulach@1334
|
287 |
* <li>Exponential patterns may not contain grouping separators.
|
jtulach@1334
|
288 |
* </ul>
|
jtulach@1334
|
289 |
*
|
jtulach@1334
|
290 |
* <h4>Rounding</h4>
|
jtulach@1334
|
291 |
*
|
jtulach@1334
|
292 |
* <code>DecimalFormat</code> provides rounding modes defined in
|
jtulach@1334
|
293 |
* {@link java.math.RoundingMode} for formatting. By default, it uses
|
jtulach@1334
|
294 |
* {@link java.math.RoundingMode#HALF_EVEN RoundingMode.HALF_EVEN}.
|
jtulach@1334
|
295 |
*
|
jtulach@1334
|
296 |
* <h4>Digits</h4>
|
jtulach@1334
|
297 |
*
|
jtulach@1334
|
298 |
* For formatting, <code>DecimalFormat</code> uses the ten consecutive
|
jtulach@1334
|
299 |
* characters starting with the localized zero digit defined in the
|
jtulach@1334
|
300 |
* <code>DecimalFormatSymbols</code> object as digits. For parsing, these
|
jtulach@1334
|
301 |
* digits as well as all Unicode decimal digits, as defined by
|
jtulach@1334
|
302 |
* {@link Character#digit Character.digit}, are recognized.
|
jtulach@1334
|
303 |
*
|
jtulach@1334
|
304 |
* <h4>Special Values</h4>
|
jtulach@1334
|
305 |
*
|
jtulach@1334
|
306 |
* <p><code>NaN</code> is formatted as a string, which typically has a single character
|
jtulach@1334
|
307 |
* <code>\uFFFD</code>. This string is determined by the
|
jtulach@1334
|
308 |
* <code>DecimalFormatSymbols</code> object. This is the only value for which
|
jtulach@1334
|
309 |
* the prefixes and suffixes are not used.
|
jtulach@1334
|
310 |
*
|
jtulach@1334
|
311 |
* <p>Infinity is formatted as a string, which typically has a single character
|
jtulach@1334
|
312 |
* <code>\u221E</code>, with the positive or negative prefixes and suffixes
|
jtulach@1334
|
313 |
* applied. The infinity string is determined by the
|
jtulach@1334
|
314 |
* <code>DecimalFormatSymbols</code> object.
|
jtulach@1334
|
315 |
*
|
jtulach@1334
|
316 |
* <p>Negative zero (<code>"-0"</code>) parses to
|
jtulach@1334
|
317 |
* <ul>
|
jtulach@1334
|
318 |
* <li><code>BigDecimal(0)</code> if <code>isParseBigDecimal()</code> is
|
jtulach@1334
|
319 |
* true,
|
jtulach@1334
|
320 |
* <li><code>Long(0)</code> if <code>isParseBigDecimal()</code> is false
|
jtulach@1334
|
321 |
* and <code>isParseIntegerOnly()</code> is true,
|
jtulach@1334
|
322 |
* <li><code>Double(-0.0)</code> if both <code>isParseBigDecimal()</code>
|
jtulach@1334
|
323 |
* and <code>isParseIntegerOnly()</code> are false.
|
jtulach@1334
|
324 |
* </ul>
|
jtulach@1334
|
325 |
*
|
jtulach@1334
|
326 |
* <h4><a name="synchronization">Synchronization</a></h4>
|
jtulach@1334
|
327 |
*
|
jtulach@1334
|
328 |
* <p>
|
jtulach@1334
|
329 |
* Decimal formats are generally not synchronized.
|
jtulach@1334
|
330 |
* It is recommended to create separate format instances for each thread.
|
jtulach@1334
|
331 |
* If multiple threads access a format concurrently, it must be synchronized
|
jtulach@1334
|
332 |
* externally.
|
jtulach@1334
|
333 |
*
|
jtulach@1334
|
334 |
* <h4>Example</h4>
|
jtulach@1334
|
335 |
*
|
jtulach@1334
|
336 |
* <blockquote><pre>
|
jtulach@1334
|
337 |
* <strong>// Print out a number using the localized number, integer, currency,
|
jtulach@1334
|
338 |
* // and percent format for each locale</strong>
|
jtulach@1334
|
339 |
* Locale[] locales = NumberFormat.getAvailableLocales();
|
jtulach@1334
|
340 |
* double myNumber = -1234.56;
|
jtulach@1334
|
341 |
* NumberFormat form;
|
jtulach@1334
|
342 |
* for (int j=0; j<4; ++j) {
|
jtulach@1334
|
343 |
* System.out.println("FORMAT");
|
jtulach@1334
|
344 |
* for (int i = 0; i < locales.length; ++i) {
|
jtulach@1334
|
345 |
* if (locales[i].getCountry().length() == 0) {
|
jtulach@1334
|
346 |
* continue; // Skip language-only locales
|
jtulach@1334
|
347 |
* }
|
jtulach@1334
|
348 |
* System.out.print(locales[i].getDisplayName());
|
jtulach@1334
|
349 |
* switch (j) {
|
jtulach@1334
|
350 |
* case 0:
|
jtulach@1334
|
351 |
* form = NumberFormat.getInstance(locales[i]); break;
|
jtulach@1334
|
352 |
* case 1:
|
jtulach@1334
|
353 |
* form = NumberFormat.getIntegerInstance(locales[i]); break;
|
jtulach@1334
|
354 |
* case 2:
|
jtulach@1334
|
355 |
* form = NumberFormat.getCurrencyInstance(locales[i]); break;
|
jtulach@1334
|
356 |
* default:
|
jtulach@1334
|
357 |
* form = NumberFormat.getPercentInstance(locales[i]); break;
|
jtulach@1334
|
358 |
* }
|
jtulach@1334
|
359 |
* if (form instanceof DecimalFormat) {
|
jtulach@1334
|
360 |
* System.out.print(": " + ((DecimalFormat) form).toPattern());
|
jtulach@1334
|
361 |
* }
|
jtulach@1334
|
362 |
* System.out.print(" -> " + form.format(myNumber));
|
jtulach@1334
|
363 |
* try {
|
jtulach@1334
|
364 |
* System.out.println(" -> " + form.parse(form.format(myNumber)));
|
jtulach@1334
|
365 |
* } catch (ParseException e) {}
|
jtulach@1334
|
366 |
* }
|
jtulach@1334
|
367 |
* }
|
jtulach@1334
|
368 |
* </pre></blockquote>
|
jtulach@1334
|
369 |
*
|
jtulach@1334
|
370 |
* @see <a href="http://java.sun.com/docs/books/tutorial/i18n/format/decimalFormat.html">Java Tutorial</a>
|
jtulach@1334
|
371 |
* @see NumberFormat
|
jtulach@1334
|
372 |
* @see DecimalFormatSymbols
|
jtulach@1334
|
373 |
* @see ParsePosition
|
jtulach@1334
|
374 |
* @author Mark Davis
|
jtulach@1334
|
375 |
* @author Alan Liu
|
jtulach@1334
|
376 |
*/
|
jtulach@1334
|
377 |
public class DecimalFormat extends NumberFormat {
|
jtulach@1334
|
378 |
|
jtulach@1334
|
379 |
/**
|
jtulach@1334
|
380 |
* Creates a DecimalFormat using the default pattern and symbols
|
jtulach@1334
|
381 |
* for the default locale. This is a convenient way to obtain a
|
jtulach@1334
|
382 |
* DecimalFormat when internationalization is not the main concern.
|
jtulach@1334
|
383 |
* <p>
|
jtulach@1334
|
384 |
* To obtain standard formats for a given locale, use the factory methods
|
jtulach@1334
|
385 |
* on NumberFormat such as getNumberInstance. These factories will
|
jtulach@1334
|
386 |
* return the most appropriate sub-class of NumberFormat for a given
|
jtulach@1334
|
387 |
* locale.
|
jtulach@1334
|
388 |
*
|
jtulach@1334
|
389 |
* @see java.text.NumberFormat#getInstance
|
jtulach@1334
|
390 |
* @see java.text.NumberFormat#getNumberInstance
|
jtulach@1334
|
391 |
* @see java.text.NumberFormat#getCurrencyInstance
|
jtulach@1334
|
392 |
* @see java.text.NumberFormat#getPercentInstance
|
jtulach@1334
|
393 |
*/
|
jtulach@1334
|
394 |
public DecimalFormat() {
|
jtulach@1334
|
395 |
Locale def = Locale.getDefault(Locale.Category.FORMAT);
|
jtulach@1334
|
396 |
// try to get the pattern from the cache
|
jtulach@1334
|
397 |
String pattern = cachedLocaleData.get(def);
|
jtulach@1334
|
398 |
if (pattern == null) { /* cache miss */
|
jtulach@1334
|
399 |
// Get the pattern for the default locale.
|
jaroslav@1339
|
400 |
// ResourceBundle rb = LocaleData.getNumberFormatData(def);
|
jaroslav@1339
|
401 |
// String[] all = rb.getStringArray("NumberPatterns");
|
jaroslav@1339
|
402 |
// pattern = all[0];
|
jaroslav@1339
|
403 |
// /* update cache */
|
jaroslav@1339
|
404 |
// cachedLocaleData.putIfAbsent(def, pattern);
|
jtulach@1334
|
405 |
}
|
jtulach@1334
|
406 |
|
jtulach@1334
|
407 |
// Always applyPattern after the symbols are set
|
jtulach@1334
|
408 |
this.symbols = new DecimalFormatSymbols(def);
|
jtulach@1334
|
409 |
applyPattern(pattern, false);
|
jtulach@1334
|
410 |
}
|
jtulach@1334
|
411 |
|
jtulach@1334
|
412 |
|
jtulach@1334
|
413 |
/**
|
jtulach@1334
|
414 |
* Creates a DecimalFormat using the given pattern and the symbols
|
jtulach@1334
|
415 |
* for the default locale. This is a convenient way to obtain a
|
jtulach@1334
|
416 |
* DecimalFormat when internationalization is not the main concern.
|
jtulach@1334
|
417 |
* <p>
|
jtulach@1334
|
418 |
* To obtain standard formats for a given locale, use the factory methods
|
jtulach@1334
|
419 |
* on NumberFormat such as getNumberInstance. These factories will
|
jtulach@1334
|
420 |
* return the most appropriate sub-class of NumberFormat for a given
|
jtulach@1334
|
421 |
* locale.
|
jtulach@1334
|
422 |
*
|
jtulach@1334
|
423 |
* @param pattern A non-localized pattern string.
|
jtulach@1334
|
424 |
* @exception NullPointerException if <code>pattern</code> is null
|
jtulach@1334
|
425 |
* @exception IllegalArgumentException if the given pattern is invalid.
|
jtulach@1334
|
426 |
* @see java.text.NumberFormat#getInstance
|
jtulach@1334
|
427 |
* @see java.text.NumberFormat#getNumberInstance
|
jtulach@1334
|
428 |
* @see java.text.NumberFormat#getCurrencyInstance
|
jtulach@1334
|
429 |
* @see java.text.NumberFormat#getPercentInstance
|
jtulach@1334
|
430 |
*/
|
jtulach@1334
|
431 |
public DecimalFormat(String pattern) {
|
jtulach@1334
|
432 |
// Always applyPattern after the symbols are set
|
jtulach@1334
|
433 |
this.symbols = new DecimalFormatSymbols(Locale.getDefault(Locale.Category.FORMAT));
|
jtulach@1334
|
434 |
applyPattern(pattern, false);
|
jtulach@1334
|
435 |
}
|
jtulach@1334
|
436 |
|
jtulach@1334
|
437 |
|
jtulach@1334
|
438 |
/**
|
jtulach@1334
|
439 |
* Creates a DecimalFormat using the given pattern and symbols.
|
jtulach@1334
|
440 |
* Use this constructor when you need to completely customize the
|
jtulach@1334
|
441 |
* behavior of the format.
|
jtulach@1334
|
442 |
* <p>
|
jtulach@1334
|
443 |
* To obtain standard formats for a given
|
jtulach@1334
|
444 |
* locale, use the factory methods on NumberFormat such as
|
jtulach@1334
|
445 |
* getInstance or getCurrencyInstance. If you need only minor adjustments
|
jtulach@1334
|
446 |
* to a standard format, you can modify the format returned by
|
jtulach@1334
|
447 |
* a NumberFormat factory method.
|
jtulach@1334
|
448 |
*
|
jtulach@1334
|
449 |
* @param pattern a non-localized pattern string
|
jtulach@1334
|
450 |
* @param symbols the set of symbols to be used
|
jtulach@1334
|
451 |
* @exception NullPointerException if any of the given arguments is null
|
jtulach@1334
|
452 |
* @exception IllegalArgumentException if the given pattern is invalid
|
jtulach@1334
|
453 |
* @see java.text.NumberFormat#getInstance
|
jtulach@1334
|
454 |
* @see java.text.NumberFormat#getNumberInstance
|
jtulach@1334
|
455 |
* @see java.text.NumberFormat#getCurrencyInstance
|
jtulach@1334
|
456 |
* @see java.text.NumberFormat#getPercentInstance
|
jtulach@1334
|
457 |
* @see java.text.DecimalFormatSymbols
|
jtulach@1334
|
458 |
*/
|
jtulach@1334
|
459 |
public DecimalFormat (String pattern, DecimalFormatSymbols symbols) {
|
jtulach@1334
|
460 |
// Always applyPattern after the symbols are set
|
jtulach@1334
|
461 |
this.symbols = (DecimalFormatSymbols)symbols.clone();
|
jtulach@1334
|
462 |
applyPattern(pattern, false);
|
jtulach@1334
|
463 |
}
|
jtulach@1334
|
464 |
|
jtulach@1334
|
465 |
|
jtulach@1334
|
466 |
// Overrides
|
jtulach@1334
|
467 |
/**
|
jtulach@1334
|
468 |
* Formats a number and appends the resulting text to the given string
|
jtulach@1334
|
469 |
* buffer.
|
jtulach@1334
|
470 |
* The number can be of any subclass of {@link java.lang.Number}.
|
jtulach@1334
|
471 |
* <p>
|
jtulach@1334
|
472 |
* This implementation uses the maximum precision permitted.
|
jtulach@1334
|
473 |
* @param number the number to format
|
jtulach@1334
|
474 |
* @param toAppendTo the <code>StringBuffer</code> to which the formatted
|
jtulach@1334
|
475 |
* text is to be appended
|
jtulach@1334
|
476 |
* @param pos On input: an alignment field, if desired.
|
jtulach@1334
|
477 |
* On output: the offsets of the alignment field.
|
jtulach@1334
|
478 |
* @return the value passed in as <code>toAppendTo</code>
|
jtulach@1334
|
479 |
* @exception IllegalArgumentException if <code>number</code> is
|
jtulach@1334
|
480 |
* null or not an instance of <code>Number</code>.
|
jtulach@1334
|
481 |
* @exception NullPointerException if <code>toAppendTo</code> or
|
jtulach@1334
|
482 |
* <code>pos</code> is null
|
jtulach@1334
|
483 |
* @exception ArithmeticException if rounding is needed with rounding
|
jtulach@1334
|
484 |
* mode being set to RoundingMode.UNNECESSARY
|
jtulach@1334
|
485 |
* @see java.text.FieldPosition
|
jtulach@1334
|
486 |
*/
|
jtulach@1334
|
487 |
public final StringBuffer format(Object number,
|
jtulach@1334
|
488 |
StringBuffer toAppendTo,
|
jtulach@1334
|
489 |
FieldPosition pos) {
|
jtulach@1334
|
490 |
if (number instanceof Long || number instanceof Integer ||
|
jtulach@1334
|
491 |
number instanceof Short || number instanceof Byte ||
|
jtulach@1334
|
492 |
number instanceof AtomicInteger ||
|
jtulach@1334
|
493 |
number instanceof AtomicLong ||
|
jtulach@1334
|
494 |
(number instanceof BigInteger &&
|
jtulach@1334
|
495 |
((BigInteger)number).bitLength () < 64)) {
|
jtulach@1334
|
496 |
return format(((Number)number).longValue(), toAppendTo, pos);
|
jtulach@1334
|
497 |
} else if (number instanceof BigDecimal) {
|
jtulach@1334
|
498 |
return format((BigDecimal)number, toAppendTo, pos);
|
jtulach@1334
|
499 |
} else if (number instanceof BigInteger) {
|
jtulach@1334
|
500 |
return format((BigInteger)number, toAppendTo, pos);
|
jtulach@1334
|
501 |
} else if (number instanceof Number) {
|
jtulach@1334
|
502 |
return format(((Number)number).doubleValue(), toAppendTo, pos);
|
jtulach@1334
|
503 |
} else {
|
jtulach@1334
|
504 |
throw new IllegalArgumentException("Cannot format given Object as a Number");
|
jtulach@1334
|
505 |
}
|
jtulach@1334
|
506 |
}
|
jtulach@1334
|
507 |
|
jtulach@1334
|
508 |
/**
|
jtulach@1334
|
509 |
* Formats a double to produce a string.
|
jtulach@1334
|
510 |
* @param number The double to format
|
jtulach@1334
|
511 |
* @param result where the text is to be appended
|
jtulach@1334
|
512 |
* @param fieldPosition On input: an alignment field, if desired.
|
jtulach@1334
|
513 |
* On output: the offsets of the alignment field.
|
jtulach@1334
|
514 |
* @exception ArithmeticException if rounding is needed with rounding
|
jtulach@1334
|
515 |
* mode being set to RoundingMode.UNNECESSARY
|
jtulach@1334
|
516 |
* @return The formatted number string
|
jtulach@1334
|
517 |
* @see java.text.FieldPosition
|
jtulach@1334
|
518 |
*/
|
jtulach@1334
|
519 |
public StringBuffer format(double number, StringBuffer result,
|
jtulach@1334
|
520 |
FieldPosition fieldPosition) {
|
jtulach@1334
|
521 |
fieldPosition.setBeginIndex(0);
|
jtulach@1334
|
522 |
fieldPosition.setEndIndex(0);
|
jtulach@1334
|
523 |
|
jtulach@1334
|
524 |
return format(number, result, fieldPosition.getFieldDelegate());
|
jtulach@1334
|
525 |
}
|
jtulach@1334
|
526 |
|
jtulach@1334
|
527 |
/**
|
jtulach@1334
|
528 |
* Formats a double to produce a string.
|
jtulach@1334
|
529 |
* @param number The double to format
|
jtulach@1334
|
530 |
* @param result where the text is to be appended
|
jtulach@1334
|
531 |
* @param delegate notified of locations of sub fields
|
jtulach@1334
|
532 |
* @exception ArithmeticException if rounding is needed with rounding
|
jtulach@1334
|
533 |
* mode being set to RoundingMode.UNNECESSARY
|
jtulach@1334
|
534 |
* @return The formatted number string
|
jtulach@1334
|
535 |
*/
|
jtulach@1334
|
536 |
private StringBuffer format(double number, StringBuffer result,
|
jtulach@1334
|
537 |
FieldDelegate delegate) {
|
jtulach@1334
|
538 |
if (Double.isNaN(number) ||
|
jtulach@1334
|
539 |
(Double.isInfinite(number) && multiplier == 0)) {
|
jtulach@1334
|
540 |
int iFieldStart = result.length();
|
jtulach@1334
|
541 |
result.append(symbols.getNaN());
|
jtulach@1334
|
542 |
delegate.formatted(INTEGER_FIELD, Field.INTEGER, Field.INTEGER,
|
jtulach@1334
|
543 |
iFieldStart, result.length(), result);
|
jtulach@1334
|
544 |
return result;
|
jtulach@1334
|
545 |
}
|
jtulach@1334
|
546 |
|
jtulach@1334
|
547 |
/* Detecting whether a double is negative is easy with the exception of
|
jtulach@1334
|
548 |
* the value -0.0. This is a double which has a zero mantissa (and
|
jtulach@1334
|
549 |
* exponent), but a negative sign bit. It is semantically distinct from
|
jtulach@1334
|
550 |
* a zero with a positive sign bit, and this distinction is important
|
jtulach@1334
|
551 |
* to certain kinds of computations. However, it's a little tricky to
|
jtulach@1334
|
552 |
* detect, since (-0.0 == 0.0) and !(-0.0 < 0.0). How then, you may
|
jtulach@1334
|
553 |
* ask, does it behave distinctly from +0.0? Well, 1/(-0.0) ==
|
jtulach@1334
|
554 |
* -Infinity. Proper detection of -0.0 is needed to deal with the
|
jtulach@1334
|
555 |
* issues raised by bugs 4106658, 4106667, and 4147706. Liu 7/6/98.
|
jtulach@1334
|
556 |
*/
|
jtulach@1334
|
557 |
boolean isNegative = ((number < 0.0) || (number == 0.0 && 1/number < 0.0)) ^ (multiplier < 0);
|
jtulach@1334
|
558 |
|
jtulach@1334
|
559 |
if (multiplier != 1) {
|
jtulach@1334
|
560 |
number *= multiplier;
|
jtulach@1334
|
561 |
}
|
jtulach@1334
|
562 |
|
jtulach@1334
|
563 |
if (Double.isInfinite(number)) {
|
jtulach@1334
|
564 |
if (isNegative) {
|
jtulach@1334
|
565 |
append(result, negativePrefix, delegate,
|
jtulach@1334
|
566 |
getNegativePrefixFieldPositions(), Field.SIGN);
|
jtulach@1334
|
567 |
} else {
|
jtulach@1334
|
568 |
append(result, positivePrefix, delegate,
|
jtulach@1334
|
569 |
getPositivePrefixFieldPositions(), Field.SIGN);
|
jtulach@1334
|
570 |
}
|
jtulach@1334
|
571 |
|
jtulach@1334
|
572 |
int iFieldStart = result.length();
|
jtulach@1334
|
573 |
result.append(symbols.getInfinity());
|
jtulach@1334
|
574 |
delegate.formatted(INTEGER_FIELD, Field.INTEGER, Field.INTEGER,
|
jtulach@1334
|
575 |
iFieldStart, result.length(), result);
|
jtulach@1334
|
576 |
|
jtulach@1334
|
577 |
if (isNegative) {
|
jtulach@1334
|
578 |
append(result, negativeSuffix, delegate,
|
jtulach@1334
|
579 |
getNegativeSuffixFieldPositions(), Field.SIGN);
|
jtulach@1334
|
580 |
} else {
|
jtulach@1334
|
581 |
append(result, positiveSuffix, delegate,
|
jtulach@1334
|
582 |
getPositiveSuffixFieldPositions(), Field.SIGN);
|
jtulach@1334
|
583 |
}
|
jtulach@1334
|
584 |
|
jtulach@1334
|
585 |
return result;
|
jtulach@1334
|
586 |
}
|
jtulach@1334
|
587 |
|
jtulach@1334
|
588 |
if (isNegative) {
|
jtulach@1334
|
589 |
number = -number;
|
jtulach@1334
|
590 |
}
|
jtulach@1334
|
591 |
|
jtulach@1334
|
592 |
// at this point we are guaranteed a nonnegative finite number.
|
jtulach@1334
|
593 |
assert(number >= 0 && !Double.isInfinite(number));
|
jtulach@1334
|
594 |
|
jtulach@1334
|
595 |
synchronized(digitList) {
|
jtulach@1334
|
596 |
int maxIntDigits = super.getMaximumIntegerDigits();
|
jtulach@1334
|
597 |
int minIntDigits = super.getMinimumIntegerDigits();
|
jtulach@1334
|
598 |
int maxFraDigits = super.getMaximumFractionDigits();
|
jtulach@1334
|
599 |
int minFraDigits = super.getMinimumFractionDigits();
|
jtulach@1334
|
600 |
|
jtulach@1334
|
601 |
digitList.set(isNegative, number, useExponentialNotation ?
|
jtulach@1334
|
602 |
maxIntDigits + maxFraDigits : maxFraDigits,
|
jtulach@1334
|
603 |
!useExponentialNotation);
|
jtulach@1334
|
604 |
return subformat(result, delegate, isNegative, false,
|
jtulach@1334
|
605 |
maxIntDigits, minIntDigits, maxFraDigits, minFraDigits);
|
jtulach@1334
|
606 |
}
|
jtulach@1334
|
607 |
}
|
jtulach@1334
|
608 |
|
jtulach@1334
|
609 |
/**
|
jtulach@1334
|
610 |
* Format a long to produce a string.
|
jtulach@1334
|
611 |
* @param number The long to format
|
jtulach@1334
|
612 |
* @param result where the text is to be appended
|
jtulach@1334
|
613 |
* @param fieldPosition On input: an alignment field, if desired.
|
jtulach@1334
|
614 |
* On output: the offsets of the alignment field.
|
jtulach@1334
|
615 |
* @exception ArithmeticException if rounding is needed with rounding
|
jtulach@1334
|
616 |
* mode being set to RoundingMode.UNNECESSARY
|
jtulach@1334
|
617 |
* @return The formatted number string
|
jtulach@1334
|
618 |
* @see java.text.FieldPosition
|
jtulach@1334
|
619 |
*/
|
jtulach@1334
|
620 |
public StringBuffer format(long number, StringBuffer result,
|
jtulach@1334
|
621 |
FieldPosition fieldPosition) {
|
jtulach@1334
|
622 |
fieldPosition.setBeginIndex(0);
|
jtulach@1334
|
623 |
fieldPosition.setEndIndex(0);
|
jtulach@1334
|
624 |
|
jtulach@1334
|
625 |
return format(number, result, fieldPosition.getFieldDelegate());
|
jtulach@1334
|
626 |
}
|
jtulach@1334
|
627 |
|
jtulach@1334
|
628 |
/**
|
jtulach@1334
|
629 |
* Format a long to produce a string.
|
jtulach@1334
|
630 |
* @param number The long to format
|
jtulach@1334
|
631 |
* @param result where the text is to be appended
|
jtulach@1334
|
632 |
* @param delegate notified of locations of sub fields
|
jtulach@1334
|
633 |
* @return The formatted number string
|
jtulach@1334
|
634 |
* @exception ArithmeticException if rounding is needed with rounding
|
jtulach@1334
|
635 |
* mode being set to RoundingMode.UNNECESSARY
|
jtulach@1334
|
636 |
* @see java.text.FieldPosition
|
jtulach@1334
|
637 |
*/
|
jtulach@1334
|
638 |
private StringBuffer format(long number, StringBuffer result,
|
jtulach@1334
|
639 |
FieldDelegate delegate) {
|
jtulach@1334
|
640 |
boolean isNegative = (number < 0);
|
jtulach@1334
|
641 |
if (isNegative) {
|
jtulach@1334
|
642 |
number = -number;
|
jtulach@1334
|
643 |
}
|
jtulach@1334
|
644 |
|
jtulach@1334
|
645 |
// In general, long values always represent real finite numbers, so
|
jtulach@1334
|
646 |
// we don't have to check for +/- Infinity or NaN. However, there
|
jtulach@1334
|
647 |
// is one case we have to be careful of: The multiplier can push
|
jtulach@1334
|
648 |
// a number near MIN_VALUE or MAX_VALUE outside the legal range. We
|
jtulach@1334
|
649 |
// check for this before multiplying, and if it happens we use
|
jtulach@1334
|
650 |
// BigInteger instead.
|
jtulach@1334
|
651 |
boolean useBigInteger = false;
|
jtulach@1334
|
652 |
if (number < 0) { // This can only happen if number == Long.MIN_VALUE.
|
jtulach@1334
|
653 |
if (multiplier != 0) {
|
jtulach@1334
|
654 |
useBigInteger = true;
|
jtulach@1334
|
655 |
}
|
jtulach@1334
|
656 |
} else if (multiplier != 1 && multiplier != 0) {
|
jtulach@1334
|
657 |
long cutoff = Long.MAX_VALUE / multiplier;
|
jtulach@1334
|
658 |
if (cutoff < 0) {
|
jtulach@1334
|
659 |
cutoff = -cutoff;
|
jtulach@1334
|
660 |
}
|
jtulach@1334
|
661 |
useBigInteger = (number > cutoff);
|
jtulach@1334
|
662 |
}
|
jtulach@1334
|
663 |
|
jtulach@1334
|
664 |
if (useBigInteger) {
|
jtulach@1334
|
665 |
if (isNegative) {
|
jtulach@1334
|
666 |
number = -number;
|
jtulach@1334
|
667 |
}
|
jtulach@1334
|
668 |
BigInteger bigIntegerValue = BigInteger.valueOf(number);
|
jtulach@1334
|
669 |
return format(bigIntegerValue, result, delegate, true);
|
jtulach@1334
|
670 |
}
|
jtulach@1334
|
671 |
|
jtulach@1334
|
672 |
number *= multiplier;
|
jtulach@1334
|
673 |
if (number == 0) {
|
jtulach@1334
|
674 |
isNegative = false;
|
jtulach@1334
|
675 |
} else {
|
jtulach@1334
|
676 |
if (multiplier < 0) {
|
jtulach@1334
|
677 |
number = -number;
|
jtulach@1334
|
678 |
isNegative = !isNegative;
|
jtulach@1334
|
679 |
}
|
jtulach@1334
|
680 |
}
|
jtulach@1334
|
681 |
|
jtulach@1334
|
682 |
synchronized(digitList) {
|
jtulach@1334
|
683 |
int maxIntDigits = super.getMaximumIntegerDigits();
|
jtulach@1334
|
684 |
int minIntDigits = super.getMinimumIntegerDigits();
|
jtulach@1334
|
685 |
int maxFraDigits = super.getMaximumFractionDigits();
|
jtulach@1334
|
686 |
int minFraDigits = super.getMinimumFractionDigits();
|
jtulach@1334
|
687 |
|
jtulach@1334
|
688 |
digitList.set(isNegative, number,
|
jtulach@1334
|
689 |
useExponentialNotation ? maxIntDigits + maxFraDigits : 0);
|
jtulach@1334
|
690 |
|
jtulach@1334
|
691 |
return subformat(result, delegate, isNegative, true,
|
jtulach@1334
|
692 |
maxIntDigits, minIntDigits, maxFraDigits, minFraDigits);
|
jtulach@1334
|
693 |
}
|
jtulach@1334
|
694 |
}
|
jtulach@1334
|
695 |
|
jtulach@1334
|
696 |
/**
|
jtulach@1334
|
697 |
* Formats a BigDecimal to produce a string.
|
jtulach@1334
|
698 |
* @param number The BigDecimal to format
|
jtulach@1334
|
699 |
* @param result where the text is to be appended
|
jtulach@1334
|
700 |
* @param fieldPosition On input: an alignment field, if desired.
|
jtulach@1334
|
701 |
* On output: the offsets of the alignment field.
|
jtulach@1334
|
702 |
* @return The formatted number string
|
jtulach@1334
|
703 |
* @exception ArithmeticException if rounding is needed with rounding
|
jtulach@1334
|
704 |
* mode being set to RoundingMode.UNNECESSARY
|
jtulach@1334
|
705 |
* @see java.text.FieldPosition
|
jtulach@1334
|
706 |
*/
|
jtulach@1334
|
707 |
private StringBuffer format(BigDecimal number, StringBuffer result,
|
jtulach@1334
|
708 |
FieldPosition fieldPosition) {
|
jtulach@1334
|
709 |
fieldPosition.setBeginIndex(0);
|
jtulach@1334
|
710 |
fieldPosition.setEndIndex(0);
|
jtulach@1334
|
711 |
return format(number, result, fieldPosition.getFieldDelegate());
|
jtulach@1334
|
712 |
}
|
jtulach@1334
|
713 |
|
jtulach@1334
|
714 |
/**
|
jtulach@1334
|
715 |
* Formats a BigDecimal to produce a string.
|
jtulach@1334
|
716 |
* @param number The BigDecimal to format
|
jtulach@1334
|
717 |
* @param result where the text is to be appended
|
jtulach@1334
|
718 |
* @param delegate notified of locations of sub fields
|
jtulach@1334
|
719 |
* @exception ArithmeticException if rounding is needed with rounding
|
jtulach@1334
|
720 |
* mode being set to RoundingMode.UNNECESSARY
|
jtulach@1334
|
721 |
* @return The formatted number string
|
jtulach@1334
|
722 |
*/
|
jtulach@1334
|
723 |
private StringBuffer format(BigDecimal number, StringBuffer result,
|
jtulach@1334
|
724 |
FieldDelegate delegate) {
|
jtulach@1334
|
725 |
if (multiplier != 1) {
|
jtulach@1334
|
726 |
number = number.multiply(getBigDecimalMultiplier());
|
jtulach@1334
|
727 |
}
|
jtulach@1334
|
728 |
boolean isNegative = number.signum() == -1;
|
jtulach@1334
|
729 |
if (isNegative) {
|
jtulach@1334
|
730 |
number = number.negate();
|
jtulach@1334
|
731 |
}
|
jtulach@1334
|
732 |
|
jtulach@1334
|
733 |
synchronized(digitList) {
|
jtulach@1334
|
734 |
int maxIntDigits = getMaximumIntegerDigits();
|
jtulach@1334
|
735 |
int minIntDigits = getMinimumIntegerDigits();
|
jtulach@1334
|
736 |
int maxFraDigits = getMaximumFractionDigits();
|
jtulach@1334
|
737 |
int minFraDigits = getMinimumFractionDigits();
|
jtulach@1334
|
738 |
int maximumDigits = maxIntDigits + maxFraDigits;
|
jtulach@1334
|
739 |
|
jtulach@1334
|
740 |
digitList.set(isNegative, number, useExponentialNotation ?
|
jtulach@1334
|
741 |
((maximumDigits < 0) ? Integer.MAX_VALUE : maximumDigits) :
|
jtulach@1334
|
742 |
maxFraDigits, !useExponentialNotation);
|
jtulach@1334
|
743 |
|
jtulach@1334
|
744 |
return subformat(result, delegate, isNegative, false,
|
jtulach@1334
|
745 |
maxIntDigits, minIntDigits, maxFraDigits, minFraDigits);
|
jtulach@1334
|
746 |
}
|
jtulach@1334
|
747 |
}
|
jtulach@1334
|
748 |
|
jtulach@1334
|
749 |
/**
|
jtulach@1334
|
750 |
* Format a BigInteger to produce a string.
|
jtulach@1334
|
751 |
* @param number The BigInteger to format
|
jtulach@1334
|
752 |
* @param result where the text is to be appended
|
jtulach@1334
|
753 |
* @param fieldPosition On input: an alignment field, if desired.
|
jtulach@1334
|
754 |
* On output: the offsets of the alignment field.
|
jtulach@1334
|
755 |
* @return The formatted number string
|
jtulach@1334
|
756 |
* @exception ArithmeticException if rounding is needed with rounding
|
jtulach@1334
|
757 |
* mode being set to RoundingMode.UNNECESSARY
|
jtulach@1334
|
758 |
* @see java.text.FieldPosition
|
jtulach@1334
|
759 |
*/
|
jtulach@1334
|
760 |
private StringBuffer format(BigInteger number, StringBuffer result,
|
jtulach@1334
|
761 |
FieldPosition fieldPosition) {
|
jtulach@1334
|
762 |
fieldPosition.setBeginIndex(0);
|
jtulach@1334
|
763 |
fieldPosition.setEndIndex(0);
|
jtulach@1334
|
764 |
|
jtulach@1334
|
765 |
return format(number, result, fieldPosition.getFieldDelegate(), false);
|
jtulach@1334
|
766 |
}
|
jtulach@1334
|
767 |
|
jtulach@1334
|
768 |
/**
|
jtulach@1334
|
769 |
* Format a BigInteger to produce a string.
|
jtulach@1334
|
770 |
* @param number The BigInteger to format
|
jtulach@1334
|
771 |
* @param result where the text is to be appended
|
jtulach@1334
|
772 |
* @param delegate notified of locations of sub fields
|
jtulach@1334
|
773 |
* @return The formatted number string
|
jtulach@1334
|
774 |
* @exception ArithmeticException if rounding is needed with rounding
|
jtulach@1334
|
775 |
* mode being set to RoundingMode.UNNECESSARY
|
jtulach@1334
|
776 |
* @see java.text.FieldPosition
|
jtulach@1334
|
777 |
*/
|
jtulach@1334
|
778 |
private StringBuffer format(BigInteger number, StringBuffer result,
|
jtulach@1334
|
779 |
FieldDelegate delegate, boolean formatLong) {
|
jtulach@1334
|
780 |
if (multiplier != 1) {
|
jtulach@1334
|
781 |
number = number.multiply(getBigIntegerMultiplier());
|
jtulach@1334
|
782 |
}
|
jtulach@1334
|
783 |
boolean isNegative = number.signum() == -1;
|
jtulach@1334
|
784 |
if (isNegative) {
|
jtulach@1334
|
785 |
number = number.negate();
|
jtulach@1334
|
786 |
}
|
jtulach@1334
|
787 |
|
jtulach@1334
|
788 |
synchronized(digitList) {
|
jtulach@1334
|
789 |
int maxIntDigits, minIntDigits, maxFraDigits, minFraDigits, maximumDigits;
|
jtulach@1334
|
790 |
if (formatLong) {
|
jtulach@1334
|
791 |
maxIntDigits = super.getMaximumIntegerDigits();
|
jtulach@1334
|
792 |
minIntDigits = super.getMinimumIntegerDigits();
|
jtulach@1334
|
793 |
maxFraDigits = super.getMaximumFractionDigits();
|
jtulach@1334
|
794 |
minFraDigits = super.getMinimumFractionDigits();
|
jtulach@1334
|
795 |
maximumDigits = maxIntDigits + maxFraDigits;
|
jtulach@1334
|
796 |
} else {
|
jtulach@1334
|
797 |
maxIntDigits = getMaximumIntegerDigits();
|
jtulach@1334
|
798 |
minIntDigits = getMinimumIntegerDigits();
|
jtulach@1334
|
799 |
maxFraDigits = getMaximumFractionDigits();
|
jtulach@1334
|
800 |
minFraDigits = getMinimumFractionDigits();
|
jtulach@1334
|
801 |
maximumDigits = maxIntDigits + maxFraDigits;
|
jtulach@1334
|
802 |
if (maximumDigits < 0) {
|
jtulach@1334
|
803 |
maximumDigits = Integer.MAX_VALUE;
|
jtulach@1334
|
804 |
}
|
jtulach@1334
|
805 |
}
|
jtulach@1334
|
806 |
|
jtulach@1334
|
807 |
digitList.set(isNegative, number,
|
jtulach@1334
|
808 |
useExponentialNotation ? maximumDigits : 0);
|
jtulach@1334
|
809 |
|
jtulach@1334
|
810 |
return subformat(result, delegate, isNegative, true,
|
jtulach@1334
|
811 |
maxIntDigits, minIntDigits, maxFraDigits, minFraDigits);
|
jtulach@1334
|
812 |
}
|
jtulach@1334
|
813 |
}
|
jtulach@1334
|
814 |
|
jtulach@1334
|
815 |
/**
|
jtulach@1334
|
816 |
* Formats an Object producing an <code>AttributedCharacterIterator</code>.
|
jtulach@1334
|
817 |
* You can use the returned <code>AttributedCharacterIterator</code>
|
jtulach@1334
|
818 |
* to build the resulting String, as well as to determine information
|
jtulach@1334
|
819 |
* about the resulting String.
|
jtulach@1334
|
820 |
* <p>
|
jtulach@1334
|
821 |
* Each attribute key of the AttributedCharacterIterator will be of type
|
jtulach@1334
|
822 |
* <code>NumberFormat.Field</code>, with the attribute value being the
|
jtulach@1334
|
823 |
* same as the attribute key.
|
jtulach@1334
|
824 |
*
|
jtulach@1334
|
825 |
* @exception NullPointerException if obj is null.
|
jtulach@1334
|
826 |
* @exception IllegalArgumentException when the Format cannot format the
|
jtulach@1334
|
827 |
* given object.
|
jtulach@1334
|
828 |
* @exception ArithmeticException if rounding is needed with rounding
|
jtulach@1334
|
829 |
* mode being set to RoundingMode.UNNECESSARY
|
jtulach@1334
|
830 |
* @param obj The object to format
|
jtulach@1334
|
831 |
* @return AttributedCharacterIterator describing the formatted value.
|
jtulach@1334
|
832 |
* @since 1.4
|
jtulach@1334
|
833 |
*/
|
jtulach@1334
|
834 |
public AttributedCharacterIterator formatToCharacterIterator(Object obj) {
|
jtulach@1334
|
835 |
CharacterIteratorFieldDelegate delegate =
|
jtulach@1334
|
836 |
new CharacterIteratorFieldDelegate();
|
jtulach@1334
|
837 |
StringBuffer sb = new StringBuffer();
|
jtulach@1334
|
838 |
|
jtulach@1334
|
839 |
if (obj instanceof Double || obj instanceof Float) {
|
jtulach@1334
|
840 |
format(((Number)obj).doubleValue(), sb, delegate);
|
jtulach@1334
|
841 |
} else if (obj instanceof Long || obj instanceof Integer ||
|
jtulach@1334
|
842 |
obj instanceof Short || obj instanceof Byte ||
|
jtulach@1334
|
843 |
obj instanceof AtomicInteger || obj instanceof AtomicLong) {
|
jtulach@1334
|
844 |
format(((Number)obj).longValue(), sb, delegate);
|
jtulach@1334
|
845 |
} else if (obj instanceof BigDecimal) {
|
jtulach@1334
|
846 |
format((BigDecimal)obj, sb, delegate);
|
jtulach@1334
|
847 |
} else if (obj instanceof BigInteger) {
|
jtulach@1334
|
848 |
format((BigInteger)obj, sb, delegate, false);
|
jtulach@1334
|
849 |
} else if (obj == null) {
|
jtulach@1334
|
850 |
throw new NullPointerException(
|
jtulach@1334
|
851 |
"formatToCharacterIterator must be passed non-null object");
|
jtulach@1334
|
852 |
} else {
|
jtulach@1334
|
853 |
throw new IllegalArgumentException(
|
jtulach@1334
|
854 |
"Cannot format given Object as a Number");
|
jtulach@1334
|
855 |
}
|
jtulach@1334
|
856 |
return delegate.getIterator(sb.toString());
|
jtulach@1334
|
857 |
}
|
jtulach@1334
|
858 |
|
jtulach@1334
|
859 |
/**
|
jtulach@1334
|
860 |
* Complete the formatting of a finite number. On entry, the digitList must
|
jtulach@1334
|
861 |
* be filled in with the correct digits.
|
jtulach@1334
|
862 |
*/
|
jtulach@1334
|
863 |
private StringBuffer subformat(StringBuffer result, FieldDelegate delegate,
|
jtulach@1334
|
864 |
boolean isNegative, boolean isInteger,
|
jtulach@1334
|
865 |
int maxIntDigits, int minIntDigits,
|
jtulach@1334
|
866 |
int maxFraDigits, int minFraDigits) {
|
jtulach@1334
|
867 |
// NOTE: This isn't required anymore because DigitList takes care of this.
|
jtulach@1334
|
868 |
//
|
jtulach@1334
|
869 |
// // The negative of the exponent represents the number of leading
|
jtulach@1334
|
870 |
// // zeros between the decimal and the first non-zero digit, for
|
jtulach@1334
|
871 |
// // a value < 0.1 (e.g., for 0.00123, -fExponent == 2). If this
|
jtulach@1334
|
872 |
// // is more than the maximum fraction digits, then we have an underflow
|
jtulach@1334
|
873 |
// // for the printed representation. We recognize this here and set
|
jtulach@1334
|
874 |
// // the DigitList representation to zero in this situation.
|
jtulach@1334
|
875 |
//
|
jtulach@1334
|
876 |
// if (-digitList.decimalAt >= getMaximumFractionDigits())
|
jtulach@1334
|
877 |
// {
|
jtulach@1334
|
878 |
// digitList.count = 0;
|
jtulach@1334
|
879 |
// }
|
jtulach@1334
|
880 |
|
jtulach@1334
|
881 |
char zero = symbols.getZeroDigit();
|
jtulach@1334
|
882 |
int zeroDelta = zero - '0'; // '0' is the DigitList representation of zero
|
jtulach@1334
|
883 |
char grouping = symbols.getGroupingSeparator();
|
jtulach@1334
|
884 |
char decimal = isCurrencyFormat ?
|
jtulach@1334
|
885 |
symbols.getMonetaryDecimalSeparator() :
|
jtulach@1334
|
886 |
symbols.getDecimalSeparator();
|
jtulach@1334
|
887 |
|
jtulach@1334
|
888 |
/* Per bug 4147706, DecimalFormat must respect the sign of numbers which
|
jtulach@1334
|
889 |
* format as zero. This allows sensible computations and preserves
|
jtulach@1334
|
890 |
* relations such as signum(1/x) = signum(x), where x is +Infinity or
|
jtulach@1334
|
891 |
* -Infinity. Prior to this fix, we always formatted zero values as if
|
jtulach@1334
|
892 |
* they were positive. Liu 7/6/98.
|
jtulach@1334
|
893 |
*/
|
jtulach@1334
|
894 |
if (digitList.isZero()) {
|
jtulach@1334
|
895 |
digitList.decimalAt = 0; // Normalize
|
jtulach@1334
|
896 |
}
|
jtulach@1334
|
897 |
|
jtulach@1334
|
898 |
if (isNegative) {
|
jtulach@1334
|
899 |
append(result, negativePrefix, delegate,
|
jtulach@1334
|
900 |
getNegativePrefixFieldPositions(), Field.SIGN);
|
jtulach@1334
|
901 |
} else {
|
jtulach@1334
|
902 |
append(result, positivePrefix, delegate,
|
jtulach@1334
|
903 |
getPositivePrefixFieldPositions(), Field.SIGN);
|
jtulach@1334
|
904 |
}
|
jtulach@1334
|
905 |
|
jtulach@1334
|
906 |
if (useExponentialNotation) {
|
jtulach@1334
|
907 |
int iFieldStart = result.length();
|
jtulach@1334
|
908 |
int iFieldEnd = -1;
|
jtulach@1334
|
909 |
int fFieldStart = -1;
|
jtulach@1334
|
910 |
|
jtulach@1334
|
911 |
// Minimum integer digits are handled in exponential format by
|
jtulach@1334
|
912 |
// adjusting the exponent. For example, 0.01234 with 3 minimum
|
jtulach@1334
|
913 |
// integer digits is "123.4E-4".
|
jtulach@1334
|
914 |
|
jtulach@1334
|
915 |
// Maximum integer digits are interpreted as indicating the
|
jtulach@1334
|
916 |
// repeating range. This is useful for engineering notation, in
|
jtulach@1334
|
917 |
// which the exponent is restricted to a multiple of 3. For
|
jtulach@1334
|
918 |
// example, 0.01234 with 3 maximum integer digits is "12.34e-3".
|
jtulach@1334
|
919 |
// If maximum integer digits are > 1 and are larger than
|
jtulach@1334
|
920 |
// minimum integer digits, then minimum integer digits are
|
jtulach@1334
|
921 |
// ignored.
|
jtulach@1334
|
922 |
int exponent = digitList.decimalAt;
|
jtulach@1334
|
923 |
int repeat = maxIntDigits;
|
jtulach@1334
|
924 |
int minimumIntegerDigits = minIntDigits;
|
jtulach@1334
|
925 |
if (repeat > 1 && repeat > minIntDigits) {
|
jtulach@1334
|
926 |
// A repeating range is defined; adjust to it as follows.
|
jtulach@1334
|
927 |
// If repeat == 3, we have 6,5,4=>3; 3,2,1=>0; 0,-1,-2=>-3;
|
jtulach@1334
|
928 |
// -3,-4,-5=>-6, etc. This takes into account that the
|
jtulach@1334
|
929 |
// exponent we have here is off by one from what we expect;
|
jtulach@1334
|
930 |
// it is for the format 0.MMMMMx10^n.
|
jtulach@1334
|
931 |
if (exponent >= 1) {
|
jtulach@1334
|
932 |
exponent = ((exponent - 1) / repeat) * repeat;
|
jtulach@1334
|
933 |
} else {
|
jtulach@1334
|
934 |
// integer division rounds towards 0
|
jtulach@1334
|
935 |
exponent = ((exponent - repeat) / repeat) * repeat;
|
jtulach@1334
|
936 |
}
|
jtulach@1334
|
937 |
minimumIntegerDigits = 1;
|
jtulach@1334
|
938 |
} else {
|
jtulach@1334
|
939 |
// No repeating range is defined; use minimum integer digits.
|
jtulach@1334
|
940 |
exponent -= minimumIntegerDigits;
|
jtulach@1334
|
941 |
}
|
jtulach@1334
|
942 |
|
jtulach@1334
|
943 |
// We now output a minimum number of digits, and more if there
|
jtulach@1334
|
944 |
// are more digits, up to the maximum number of digits. We
|
jtulach@1334
|
945 |
// place the decimal point after the "integer" digits, which
|
jtulach@1334
|
946 |
// are the first (decimalAt - exponent) digits.
|
jtulach@1334
|
947 |
int minimumDigits = minIntDigits + minFraDigits;
|
jtulach@1334
|
948 |
if (minimumDigits < 0) { // overflow?
|
jtulach@1334
|
949 |
minimumDigits = Integer.MAX_VALUE;
|
jtulach@1334
|
950 |
}
|
jtulach@1334
|
951 |
|
jtulach@1334
|
952 |
// The number of integer digits is handled specially if the number
|
jtulach@1334
|
953 |
// is zero, since then there may be no digits.
|
jtulach@1334
|
954 |
int integerDigits = digitList.isZero() ? minimumIntegerDigits :
|
jtulach@1334
|
955 |
digitList.decimalAt - exponent;
|
jtulach@1334
|
956 |
if (minimumDigits < integerDigits) {
|
jtulach@1334
|
957 |
minimumDigits = integerDigits;
|
jtulach@1334
|
958 |
}
|
jtulach@1334
|
959 |
int totalDigits = digitList.count;
|
jtulach@1334
|
960 |
if (minimumDigits > totalDigits) {
|
jtulach@1334
|
961 |
totalDigits = minimumDigits;
|
jtulach@1334
|
962 |
}
|
jtulach@1334
|
963 |
boolean addedDecimalSeparator = false;
|
jtulach@1334
|
964 |
|
jtulach@1334
|
965 |
for (int i=0; i<totalDigits; ++i) {
|
jtulach@1334
|
966 |
if (i == integerDigits) {
|
jtulach@1334
|
967 |
// Record field information for caller.
|
jtulach@1334
|
968 |
iFieldEnd = result.length();
|
jtulach@1334
|
969 |
|
jtulach@1334
|
970 |
result.append(decimal);
|
jtulach@1334
|
971 |
addedDecimalSeparator = true;
|
jtulach@1334
|
972 |
|
jtulach@1334
|
973 |
// Record field information for caller.
|
jtulach@1334
|
974 |
fFieldStart = result.length();
|
jtulach@1334
|
975 |
}
|
jtulach@1334
|
976 |
result.append((i < digitList.count) ?
|
jtulach@1334
|
977 |
(char)(digitList.digits[i] + zeroDelta) :
|
jtulach@1334
|
978 |
zero);
|
jtulach@1334
|
979 |
}
|
jtulach@1334
|
980 |
|
jtulach@1334
|
981 |
if (decimalSeparatorAlwaysShown && totalDigits == integerDigits) {
|
jtulach@1334
|
982 |
// Record field information for caller.
|
jtulach@1334
|
983 |
iFieldEnd = result.length();
|
jtulach@1334
|
984 |
|
jtulach@1334
|
985 |
result.append(decimal);
|
jtulach@1334
|
986 |
addedDecimalSeparator = true;
|
jtulach@1334
|
987 |
|
jtulach@1334
|
988 |
// Record field information for caller.
|
jtulach@1334
|
989 |
fFieldStart = result.length();
|
jtulach@1334
|
990 |
}
|
jtulach@1334
|
991 |
|
jtulach@1334
|
992 |
// Record field information
|
jtulach@1334
|
993 |
if (iFieldEnd == -1) {
|
jtulach@1334
|
994 |
iFieldEnd = result.length();
|
jtulach@1334
|
995 |
}
|
jtulach@1334
|
996 |
delegate.formatted(INTEGER_FIELD, Field.INTEGER, Field.INTEGER,
|
jtulach@1334
|
997 |
iFieldStart, iFieldEnd, result);
|
jtulach@1334
|
998 |
if (addedDecimalSeparator) {
|
jtulach@1334
|
999 |
delegate.formatted(Field.DECIMAL_SEPARATOR,
|
jtulach@1334
|
1000 |
Field.DECIMAL_SEPARATOR,
|
jtulach@1334
|
1001 |
iFieldEnd, fFieldStart, result);
|
jtulach@1334
|
1002 |
}
|
jtulach@1334
|
1003 |
if (fFieldStart == -1) {
|
jtulach@1334
|
1004 |
fFieldStart = result.length();
|
jtulach@1334
|
1005 |
}
|
jtulach@1334
|
1006 |
delegate.formatted(FRACTION_FIELD, Field.FRACTION, Field.FRACTION,
|
jtulach@1334
|
1007 |
fFieldStart, result.length(), result);
|
jtulach@1334
|
1008 |
|
jtulach@1334
|
1009 |
// The exponent is output using the pattern-specified minimum
|
jtulach@1334
|
1010 |
// exponent digits. There is no maximum limit to the exponent
|
jtulach@1334
|
1011 |
// digits, since truncating the exponent would result in an
|
jtulach@1334
|
1012 |
// unacceptable inaccuracy.
|
jtulach@1334
|
1013 |
int fieldStart = result.length();
|
jtulach@1334
|
1014 |
|
jtulach@1334
|
1015 |
result.append(symbols.getExponentSeparator());
|
jtulach@1334
|
1016 |
|
jtulach@1334
|
1017 |
delegate.formatted(Field.EXPONENT_SYMBOL, Field.EXPONENT_SYMBOL,
|
jtulach@1334
|
1018 |
fieldStart, result.length(), result);
|
jtulach@1334
|
1019 |
|
jtulach@1334
|
1020 |
// For zero values, we force the exponent to zero. We
|
jtulach@1334
|
1021 |
// must do this here, and not earlier, because the value
|
jtulach@1334
|
1022 |
// is used to determine integer digit count above.
|
jtulach@1334
|
1023 |
if (digitList.isZero()) {
|
jtulach@1334
|
1024 |
exponent = 0;
|
jtulach@1334
|
1025 |
}
|
jtulach@1334
|
1026 |
|
jtulach@1334
|
1027 |
boolean negativeExponent = exponent < 0;
|
jtulach@1334
|
1028 |
if (negativeExponent) {
|
jtulach@1334
|
1029 |
exponent = -exponent;
|
jtulach@1334
|
1030 |
fieldStart = result.length();
|
jtulach@1334
|
1031 |
result.append(symbols.getMinusSign());
|
jtulach@1334
|
1032 |
delegate.formatted(Field.EXPONENT_SIGN, Field.EXPONENT_SIGN,
|
jtulach@1334
|
1033 |
fieldStart, result.length(), result);
|
jtulach@1334
|
1034 |
}
|
jtulach@1334
|
1035 |
digitList.set(negativeExponent, exponent);
|
jtulach@1334
|
1036 |
|
jtulach@1334
|
1037 |
int eFieldStart = result.length();
|
jtulach@1334
|
1038 |
|
jtulach@1334
|
1039 |
for (int i=digitList.decimalAt; i<minExponentDigits; ++i) {
|
jtulach@1334
|
1040 |
result.append(zero);
|
jtulach@1334
|
1041 |
}
|
jtulach@1334
|
1042 |
for (int i=0; i<digitList.decimalAt; ++i) {
|
jtulach@1334
|
1043 |
result.append((i < digitList.count) ?
|
jtulach@1334
|
1044 |
(char)(digitList.digits[i] + zeroDelta) : zero);
|
jtulach@1334
|
1045 |
}
|
jtulach@1334
|
1046 |
delegate.formatted(Field.EXPONENT, Field.EXPONENT, eFieldStart,
|
jtulach@1334
|
1047 |
result.length(), result);
|
jtulach@1334
|
1048 |
} else {
|
jtulach@1334
|
1049 |
int iFieldStart = result.length();
|
jtulach@1334
|
1050 |
|
jtulach@1334
|
1051 |
// Output the integer portion. Here 'count' is the total
|
jtulach@1334
|
1052 |
// number of integer digits we will display, including both
|
jtulach@1334
|
1053 |
// leading zeros required to satisfy getMinimumIntegerDigits,
|
jtulach@1334
|
1054 |
// and actual digits present in the number.
|
jtulach@1334
|
1055 |
int count = minIntDigits;
|
jtulach@1334
|
1056 |
int digitIndex = 0; // Index into digitList.fDigits[]
|
jtulach@1334
|
1057 |
if (digitList.decimalAt > 0 && count < digitList.decimalAt) {
|
jtulach@1334
|
1058 |
count = digitList.decimalAt;
|
jtulach@1334
|
1059 |
}
|
jtulach@1334
|
1060 |
|
jtulach@1334
|
1061 |
// Handle the case where getMaximumIntegerDigits() is smaller
|
jtulach@1334
|
1062 |
// than the real number of integer digits. If this is so, we
|
jtulach@1334
|
1063 |
// output the least significant max integer digits. For example,
|
jtulach@1334
|
1064 |
// the value 1997 printed with 2 max integer digits is just "97".
|
jtulach@1334
|
1065 |
if (count > maxIntDigits) {
|
jtulach@1334
|
1066 |
count = maxIntDigits;
|
jtulach@1334
|
1067 |
digitIndex = digitList.decimalAt - count;
|
jtulach@1334
|
1068 |
}
|
jtulach@1334
|
1069 |
|
jtulach@1334
|
1070 |
int sizeBeforeIntegerPart = result.length();
|
jtulach@1334
|
1071 |
for (int i=count-1; i>=0; --i) {
|
jtulach@1334
|
1072 |
if (i < digitList.decimalAt && digitIndex < digitList.count) {
|
jtulach@1334
|
1073 |
// Output a real digit
|
jtulach@1334
|
1074 |
result.append((char)(digitList.digits[digitIndex++] + zeroDelta));
|
jtulach@1334
|
1075 |
} else {
|
jtulach@1334
|
1076 |
// Output a leading zero
|
jtulach@1334
|
1077 |
result.append(zero);
|
jtulach@1334
|
1078 |
}
|
jtulach@1334
|
1079 |
|
jtulach@1334
|
1080 |
// Output grouping separator if necessary. Don't output a
|
jtulach@1334
|
1081 |
// grouping separator if i==0 though; that's at the end of
|
jtulach@1334
|
1082 |
// the integer part.
|
jtulach@1334
|
1083 |
if (isGroupingUsed() && i>0 && (groupingSize != 0) &&
|
jtulach@1334
|
1084 |
(i % groupingSize == 0)) {
|
jtulach@1334
|
1085 |
int gStart = result.length();
|
jtulach@1334
|
1086 |
result.append(grouping);
|
jtulach@1334
|
1087 |
delegate.formatted(Field.GROUPING_SEPARATOR,
|
jtulach@1334
|
1088 |
Field.GROUPING_SEPARATOR, gStart,
|
jtulach@1334
|
1089 |
result.length(), result);
|
jtulach@1334
|
1090 |
}
|
jtulach@1334
|
1091 |
}
|
jtulach@1334
|
1092 |
|
jtulach@1334
|
1093 |
// Determine whether or not there are any printable fractional
|
jtulach@1334
|
1094 |
// digits. If we've used up the digits we know there aren't.
|
jtulach@1334
|
1095 |
boolean fractionPresent = (minFraDigits > 0) ||
|
jtulach@1334
|
1096 |
(!isInteger && digitIndex < digitList.count);
|
jtulach@1334
|
1097 |
|
jtulach@1334
|
1098 |
// If there is no fraction present, and we haven't printed any
|
jtulach@1334
|
1099 |
// integer digits, then print a zero. Otherwise we won't print
|
jtulach@1334
|
1100 |
// _any_ digits, and we won't be able to parse this string.
|
jtulach@1334
|
1101 |
if (!fractionPresent && result.length() == sizeBeforeIntegerPart) {
|
jtulach@1334
|
1102 |
result.append(zero);
|
jtulach@1334
|
1103 |
}
|
jtulach@1334
|
1104 |
|
jtulach@1334
|
1105 |
delegate.formatted(INTEGER_FIELD, Field.INTEGER, Field.INTEGER,
|
jtulach@1334
|
1106 |
iFieldStart, result.length(), result);
|
jtulach@1334
|
1107 |
|
jtulach@1334
|
1108 |
// Output the decimal separator if we always do so.
|
jtulach@1334
|
1109 |
int sStart = result.length();
|
jtulach@1334
|
1110 |
if (decimalSeparatorAlwaysShown || fractionPresent) {
|
jtulach@1334
|
1111 |
result.append(decimal);
|
jtulach@1334
|
1112 |
}
|
jtulach@1334
|
1113 |
|
jtulach@1334
|
1114 |
if (sStart != result.length()) {
|
jtulach@1334
|
1115 |
delegate.formatted(Field.DECIMAL_SEPARATOR,
|
jtulach@1334
|
1116 |
Field.DECIMAL_SEPARATOR,
|
jtulach@1334
|
1117 |
sStart, result.length(), result);
|
jtulach@1334
|
1118 |
}
|
jtulach@1334
|
1119 |
int fFieldStart = result.length();
|
jtulach@1334
|
1120 |
|
jtulach@1334
|
1121 |
for (int i=0; i < maxFraDigits; ++i) {
|
jtulach@1334
|
1122 |
// Here is where we escape from the loop. We escape if we've
|
jtulach@1334
|
1123 |
// output the maximum fraction digits (specified in the for
|
jtulach@1334
|
1124 |
// expression above).
|
jtulach@1334
|
1125 |
// We also stop when we've output the minimum digits and either:
|
jtulach@1334
|
1126 |
// we have an integer, so there is no fractional stuff to
|
jtulach@1334
|
1127 |
// display, or we're out of significant digits.
|
jtulach@1334
|
1128 |
if (i >= minFraDigits &&
|
jtulach@1334
|
1129 |
(isInteger || digitIndex >= digitList.count)) {
|
jtulach@1334
|
1130 |
break;
|
jtulach@1334
|
1131 |
}
|
jtulach@1334
|
1132 |
|
jtulach@1334
|
1133 |
// Output leading fractional zeros. These are zeros that come
|
jtulach@1334
|
1134 |
// after the decimal but before any significant digits. These
|
jtulach@1334
|
1135 |
// are only output if abs(number being formatted) < 1.0.
|
jtulach@1334
|
1136 |
if (-1-i > (digitList.decimalAt-1)) {
|
jtulach@1334
|
1137 |
result.append(zero);
|
jtulach@1334
|
1138 |
continue;
|
jtulach@1334
|
1139 |
}
|
jtulach@1334
|
1140 |
|
jtulach@1334
|
1141 |
// Output a digit, if we have any precision left, or a
|
jtulach@1334
|
1142 |
// zero if we don't. We don't want to output noise digits.
|
jtulach@1334
|
1143 |
if (!isInteger && digitIndex < digitList.count) {
|
jtulach@1334
|
1144 |
result.append((char)(digitList.digits[digitIndex++] + zeroDelta));
|
jtulach@1334
|
1145 |
} else {
|
jtulach@1334
|
1146 |
result.append(zero);
|
jtulach@1334
|
1147 |
}
|
jtulach@1334
|
1148 |
}
|
jtulach@1334
|
1149 |
|
jtulach@1334
|
1150 |
// Record field information for caller.
|
jtulach@1334
|
1151 |
delegate.formatted(FRACTION_FIELD, Field.FRACTION, Field.FRACTION,
|
jtulach@1334
|
1152 |
fFieldStart, result.length(), result);
|
jtulach@1334
|
1153 |
}
|
jtulach@1334
|
1154 |
|
jtulach@1334
|
1155 |
if (isNegative) {
|
jtulach@1334
|
1156 |
append(result, negativeSuffix, delegate,
|
jtulach@1334
|
1157 |
getNegativeSuffixFieldPositions(), Field.SIGN);
|
jtulach@1334
|
1158 |
}
|
jtulach@1334
|
1159 |
else {
|
jtulach@1334
|
1160 |
append(result, positiveSuffix, delegate,
|
jtulach@1334
|
1161 |
getPositiveSuffixFieldPositions(), Field.SIGN);
|
jtulach@1334
|
1162 |
}
|
jtulach@1334
|
1163 |
|
jtulach@1334
|
1164 |
return result;
|
jtulach@1334
|
1165 |
}
|
jtulach@1334
|
1166 |
|
jtulach@1334
|
1167 |
/**
|
jtulach@1334
|
1168 |
* Appends the String <code>string</code> to <code>result</code>.
|
jtulach@1334
|
1169 |
* <code>delegate</code> is notified of all the
|
jtulach@1334
|
1170 |
* <code>FieldPosition</code>s in <code>positions</code>.
|
jtulach@1334
|
1171 |
* <p>
|
jtulach@1334
|
1172 |
* If one of the <code>FieldPosition</code>s in <code>positions</code>
|
jtulach@1334
|
1173 |
* identifies a <code>SIGN</code> attribute, it is mapped to
|
jtulach@1334
|
1174 |
* <code>signAttribute</code>. This is used
|
jtulach@1334
|
1175 |
* to map the <code>SIGN</code> attribute to the <code>EXPONENT</code>
|
jtulach@1334
|
1176 |
* attribute as necessary.
|
jtulach@1334
|
1177 |
* <p>
|
jtulach@1334
|
1178 |
* This is used by <code>subformat</code> to add the prefix/suffix.
|
jtulach@1334
|
1179 |
*/
|
jtulach@1334
|
1180 |
private void append(StringBuffer result, String string,
|
jtulach@1334
|
1181 |
FieldDelegate delegate,
|
jtulach@1334
|
1182 |
FieldPosition[] positions,
|
jtulach@1334
|
1183 |
Format.Field signAttribute) {
|
jtulach@1334
|
1184 |
int start = result.length();
|
jtulach@1334
|
1185 |
|
jtulach@1334
|
1186 |
if (string.length() > 0) {
|
jtulach@1334
|
1187 |
result.append(string);
|
jtulach@1334
|
1188 |
for (int counter = 0, max = positions.length; counter < max;
|
jtulach@1334
|
1189 |
counter++) {
|
jtulach@1334
|
1190 |
FieldPosition fp = positions[counter];
|
jtulach@1334
|
1191 |
Format.Field attribute = fp.getFieldAttribute();
|
jtulach@1334
|
1192 |
|
jtulach@1334
|
1193 |
if (attribute == Field.SIGN) {
|
jtulach@1334
|
1194 |
attribute = signAttribute;
|
jtulach@1334
|
1195 |
}
|
jtulach@1334
|
1196 |
delegate.formatted(attribute, attribute,
|
jtulach@1334
|
1197 |
start + fp.getBeginIndex(),
|
jtulach@1334
|
1198 |
start + fp.getEndIndex(), result);
|
jtulach@1334
|
1199 |
}
|
jtulach@1334
|
1200 |
}
|
jtulach@1334
|
1201 |
}
|
jtulach@1334
|
1202 |
|
jtulach@1334
|
1203 |
/**
|
jtulach@1334
|
1204 |
* Parses text from a string to produce a <code>Number</code>.
|
jtulach@1334
|
1205 |
* <p>
|
jtulach@1334
|
1206 |
* The method attempts to parse text starting at the index given by
|
jtulach@1334
|
1207 |
* <code>pos</code>.
|
jtulach@1334
|
1208 |
* If parsing succeeds, then the index of <code>pos</code> is updated
|
jtulach@1334
|
1209 |
* to the index after the last character used (parsing does not necessarily
|
jtulach@1334
|
1210 |
* use all characters up to the end of the string), and the parsed
|
jtulach@1334
|
1211 |
* number is returned. The updated <code>pos</code> can be used to
|
jtulach@1334
|
1212 |
* indicate the starting point for the next call to this method.
|
jtulach@1334
|
1213 |
* If an error occurs, then the index of <code>pos</code> is not
|
jtulach@1334
|
1214 |
* changed, the error index of <code>pos</code> is set to the index of
|
jtulach@1334
|
1215 |
* the character where the error occurred, and null is returned.
|
jtulach@1334
|
1216 |
* <p>
|
jtulach@1334
|
1217 |
* The subclass returned depends on the value of {@link #isParseBigDecimal}
|
jtulach@1334
|
1218 |
* as well as on the string being parsed.
|
jtulach@1334
|
1219 |
* <ul>
|
jtulach@1334
|
1220 |
* <li>If <code>isParseBigDecimal()</code> is false (the default),
|
jtulach@1334
|
1221 |
* most integer values are returned as <code>Long</code>
|
jtulach@1334
|
1222 |
* objects, no matter how they are written: <code>"17"</code> and
|
jtulach@1334
|
1223 |
* <code>"17.000"</code> both parse to <code>Long(17)</code>.
|
jtulach@1334
|
1224 |
* Values that cannot fit into a <code>Long</code> are returned as
|
jtulach@1334
|
1225 |
* <code>Double</code>s. This includes values with a fractional part,
|
jtulach@1334
|
1226 |
* infinite values, <code>NaN</code>, and the value -0.0.
|
jtulach@1334
|
1227 |
* <code>DecimalFormat</code> does <em>not</em> decide whether to
|
jtulach@1334
|
1228 |
* return a <code>Double</code> or a <code>Long</code> based on the
|
jtulach@1334
|
1229 |
* presence of a decimal separator in the source string. Doing so
|
jtulach@1334
|
1230 |
* would prevent integers that overflow the mantissa of a double,
|
jtulach@1334
|
1231 |
* such as <code>"-9,223,372,036,854,775,808.00"</code>, from being
|
jtulach@1334
|
1232 |
* parsed accurately.
|
jtulach@1334
|
1233 |
* <p>
|
jtulach@1334
|
1234 |
* Callers may use the <code>Number</code> methods
|
jtulach@1334
|
1235 |
* <code>doubleValue</code>, <code>longValue</code>, etc., to obtain
|
jtulach@1334
|
1236 |
* the type they want.
|
jtulach@1334
|
1237 |
* <li>If <code>isParseBigDecimal()</code> is true, values are returned
|
jtulach@1334
|
1238 |
* as <code>BigDecimal</code> objects. The values are the ones
|
jtulach@1334
|
1239 |
* constructed by {@link java.math.BigDecimal#BigDecimal(String)}
|
jtulach@1334
|
1240 |
* for corresponding strings in locale-independent format. The
|
jtulach@1334
|
1241 |
* special cases negative and positive infinity and NaN are returned
|
jtulach@1334
|
1242 |
* as <code>Double</code> instances holding the values of the
|
jtulach@1334
|
1243 |
* corresponding <code>Double</code> constants.
|
jtulach@1334
|
1244 |
* </ul>
|
jtulach@1334
|
1245 |
* <p>
|
jtulach@1334
|
1246 |
* <code>DecimalFormat</code> parses all Unicode characters that represent
|
jtulach@1334
|
1247 |
* decimal digits, as defined by <code>Character.digit()</code>. In
|
jtulach@1334
|
1248 |
* addition, <code>DecimalFormat</code> also recognizes as digits the ten
|
jtulach@1334
|
1249 |
* consecutive characters starting with the localized zero digit defined in
|
jtulach@1334
|
1250 |
* the <code>DecimalFormatSymbols</code> object.
|
jtulach@1334
|
1251 |
*
|
jtulach@1334
|
1252 |
* @param text the string to be parsed
|
jtulach@1334
|
1253 |
* @param pos A <code>ParsePosition</code> object with index and error
|
jtulach@1334
|
1254 |
* index information as described above.
|
jtulach@1334
|
1255 |
* @return the parsed value, or <code>null</code> if the parse fails
|
jtulach@1334
|
1256 |
* @exception NullPointerException if <code>text</code> or
|
jtulach@1334
|
1257 |
* <code>pos</code> is null.
|
jtulach@1334
|
1258 |
*/
|
jtulach@1334
|
1259 |
public Number parse(String text, ParsePosition pos) {
|
jtulach@1334
|
1260 |
// special case NaN
|
jtulach@1334
|
1261 |
if (text.regionMatches(pos.index, symbols.getNaN(), 0, symbols.getNaN().length())) {
|
jtulach@1334
|
1262 |
pos.index = pos.index + symbols.getNaN().length();
|
jtulach@1334
|
1263 |
return new Double(Double.NaN);
|
jtulach@1334
|
1264 |
}
|
jtulach@1334
|
1265 |
|
jtulach@1334
|
1266 |
boolean[] status = new boolean[STATUS_LENGTH];
|
jtulach@1334
|
1267 |
if (!subparse(text, pos, positivePrefix, negativePrefix, digitList, false, status)) {
|
jtulach@1334
|
1268 |
return null;
|
jtulach@1334
|
1269 |
}
|
jtulach@1334
|
1270 |
|
jtulach@1334
|
1271 |
// special case INFINITY
|
jtulach@1334
|
1272 |
if (status[STATUS_INFINITE]) {
|
jtulach@1334
|
1273 |
if (status[STATUS_POSITIVE] == (multiplier >= 0)) {
|
jtulach@1334
|
1274 |
return new Double(Double.POSITIVE_INFINITY);
|
jtulach@1334
|
1275 |
} else {
|
jtulach@1334
|
1276 |
return new Double(Double.NEGATIVE_INFINITY);
|
jtulach@1334
|
1277 |
}
|
jtulach@1334
|
1278 |
}
|
jtulach@1334
|
1279 |
|
jtulach@1334
|
1280 |
if (multiplier == 0) {
|
jtulach@1334
|
1281 |
if (digitList.isZero()) {
|
jtulach@1334
|
1282 |
return new Double(Double.NaN);
|
jtulach@1334
|
1283 |
} else if (status[STATUS_POSITIVE]) {
|
jtulach@1334
|
1284 |
return new Double(Double.POSITIVE_INFINITY);
|
jtulach@1334
|
1285 |
} else {
|
jtulach@1334
|
1286 |
return new Double(Double.NEGATIVE_INFINITY);
|
jtulach@1334
|
1287 |
}
|
jtulach@1334
|
1288 |
}
|
jtulach@1334
|
1289 |
|
jtulach@1334
|
1290 |
if (isParseBigDecimal()) {
|
jtulach@1334
|
1291 |
BigDecimal bigDecimalResult = digitList.getBigDecimal();
|
jtulach@1334
|
1292 |
|
jtulach@1334
|
1293 |
if (multiplier != 1) {
|
jtulach@1334
|
1294 |
try {
|
jtulach@1334
|
1295 |
bigDecimalResult = bigDecimalResult.divide(getBigDecimalMultiplier());
|
jtulach@1334
|
1296 |
}
|
jtulach@1334
|
1297 |
catch (ArithmeticException e) { // non-terminating decimal expansion
|
jtulach@1334
|
1298 |
bigDecimalResult = bigDecimalResult.divide(getBigDecimalMultiplier(), roundingMode);
|
jtulach@1334
|
1299 |
}
|
jtulach@1334
|
1300 |
}
|
jtulach@1334
|
1301 |
|
jtulach@1334
|
1302 |
if (!status[STATUS_POSITIVE]) {
|
jtulach@1334
|
1303 |
bigDecimalResult = bigDecimalResult.negate();
|
jtulach@1334
|
1304 |
}
|
jtulach@1334
|
1305 |
return bigDecimalResult;
|
jtulach@1334
|
1306 |
} else {
|
jtulach@1334
|
1307 |
boolean gotDouble = true;
|
jtulach@1334
|
1308 |
boolean gotLongMinimum = false;
|
jtulach@1334
|
1309 |
double doubleResult = 0.0;
|
jtulach@1334
|
1310 |
long longResult = 0;
|
jtulach@1334
|
1311 |
|
jtulach@1334
|
1312 |
// Finally, have DigitList parse the digits into a value.
|
jtulach@1334
|
1313 |
if (digitList.fitsIntoLong(status[STATUS_POSITIVE], isParseIntegerOnly())) {
|
jtulach@1334
|
1314 |
gotDouble = false;
|
jtulach@1334
|
1315 |
longResult = digitList.getLong();
|
jtulach@1334
|
1316 |
if (longResult < 0) { // got Long.MIN_VALUE
|
jtulach@1334
|
1317 |
gotLongMinimum = true;
|
jtulach@1334
|
1318 |
}
|
jtulach@1334
|
1319 |
} else {
|
jtulach@1334
|
1320 |
doubleResult = digitList.getDouble();
|
jtulach@1334
|
1321 |
}
|
jtulach@1334
|
1322 |
|
jtulach@1334
|
1323 |
// Divide by multiplier. We have to be careful here not to do
|
jtulach@1334
|
1324 |
// unneeded conversions between double and long.
|
jtulach@1334
|
1325 |
if (multiplier != 1) {
|
jtulach@1334
|
1326 |
if (gotDouble) {
|
jtulach@1334
|
1327 |
doubleResult /= multiplier;
|
jtulach@1334
|
1328 |
} else {
|
jtulach@1334
|
1329 |
// Avoid converting to double if we can
|
jtulach@1334
|
1330 |
if (longResult % multiplier == 0) {
|
jtulach@1334
|
1331 |
longResult /= multiplier;
|
jtulach@1334
|
1332 |
} else {
|
jtulach@1334
|
1333 |
doubleResult = ((double)longResult) / multiplier;
|
jtulach@1334
|
1334 |
gotDouble = true;
|
jtulach@1334
|
1335 |
}
|
jtulach@1334
|
1336 |
}
|
jtulach@1334
|
1337 |
}
|
jtulach@1334
|
1338 |
|
jtulach@1334
|
1339 |
if (!status[STATUS_POSITIVE] && !gotLongMinimum) {
|
jtulach@1334
|
1340 |
doubleResult = -doubleResult;
|
jtulach@1334
|
1341 |
longResult = -longResult;
|
jtulach@1334
|
1342 |
}
|
jtulach@1334
|
1343 |
|
jtulach@1334
|
1344 |
// At this point, if we divided the result by the multiplier, the
|
jtulach@1334
|
1345 |
// result may fit into a long. We check for this case and return
|
jtulach@1334
|
1346 |
// a long if possible.
|
jtulach@1334
|
1347 |
// We must do this AFTER applying the negative (if appropriate)
|
jtulach@1334
|
1348 |
// in order to handle the case of LONG_MIN; otherwise, if we do
|
jtulach@1334
|
1349 |
// this with a positive value -LONG_MIN, the double is > 0, but
|
jtulach@1334
|
1350 |
// the long is < 0. We also must retain a double in the case of
|
jtulach@1334
|
1351 |
// -0.0, which will compare as == to a long 0 cast to a double
|
jtulach@1334
|
1352 |
// (bug 4162852).
|
jtulach@1334
|
1353 |
if (multiplier != 1 && gotDouble) {
|
jtulach@1334
|
1354 |
longResult = (long)doubleResult;
|
jtulach@1334
|
1355 |
gotDouble = ((doubleResult != (double)longResult) ||
|
jtulach@1334
|
1356 |
(doubleResult == 0.0 && 1/doubleResult < 0.0)) &&
|
jtulach@1334
|
1357 |
!isParseIntegerOnly();
|
jtulach@1334
|
1358 |
}
|
jtulach@1334
|
1359 |
|
jtulach@1334
|
1360 |
return gotDouble ?
|
jtulach@1334
|
1361 |
(Number)new Double(doubleResult) : (Number)new Long(longResult);
|
jtulach@1334
|
1362 |
}
|
jtulach@1334
|
1363 |
}
|
jtulach@1334
|
1364 |
|
jtulach@1334
|
1365 |
/**
|
jtulach@1334
|
1366 |
* Return a BigInteger multiplier.
|
jtulach@1334
|
1367 |
*/
|
jtulach@1334
|
1368 |
private BigInteger getBigIntegerMultiplier() {
|
jtulach@1334
|
1369 |
if (bigIntegerMultiplier == null) {
|
jtulach@1334
|
1370 |
bigIntegerMultiplier = BigInteger.valueOf(multiplier);
|
jtulach@1334
|
1371 |
}
|
jtulach@1334
|
1372 |
return bigIntegerMultiplier;
|
jtulach@1334
|
1373 |
}
|
jtulach@1334
|
1374 |
private transient BigInteger bigIntegerMultiplier;
|
jtulach@1334
|
1375 |
|
jtulach@1334
|
1376 |
/**
|
jtulach@1334
|
1377 |
* Return a BigDecimal multiplier.
|
jtulach@1334
|
1378 |
*/
|
jtulach@1334
|
1379 |
private BigDecimal getBigDecimalMultiplier() {
|
jtulach@1334
|
1380 |
if (bigDecimalMultiplier == null) {
|
jtulach@1334
|
1381 |
bigDecimalMultiplier = new BigDecimal(multiplier);
|
jtulach@1334
|
1382 |
}
|
jtulach@1334
|
1383 |
return bigDecimalMultiplier;
|
jtulach@1334
|
1384 |
}
|
jtulach@1334
|
1385 |
private transient BigDecimal bigDecimalMultiplier;
|
jtulach@1334
|
1386 |
|
jtulach@1334
|
1387 |
private static final int STATUS_INFINITE = 0;
|
jtulach@1334
|
1388 |
private static final int STATUS_POSITIVE = 1;
|
jtulach@1334
|
1389 |
private static final int STATUS_LENGTH = 2;
|
jtulach@1334
|
1390 |
|
jtulach@1334
|
1391 |
/**
|
jtulach@1334
|
1392 |
* Parse the given text into a number. The text is parsed beginning at
|
jtulach@1334
|
1393 |
* parsePosition, until an unparseable character is seen.
|
jtulach@1334
|
1394 |
* @param text The string to parse.
|
jtulach@1334
|
1395 |
* @param parsePosition The position at which to being parsing. Upon
|
jtulach@1334
|
1396 |
* return, the first unparseable character.
|
jtulach@1334
|
1397 |
* @param digits The DigitList to set to the parsed value.
|
jtulach@1334
|
1398 |
* @param isExponent If true, parse an exponent. This means no
|
jtulach@1334
|
1399 |
* infinite values and integer only.
|
jtulach@1334
|
1400 |
* @param status Upon return contains boolean status flags indicating
|
jtulach@1334
|
1401 |
* whether the value was infinite and whether it was positive.
|
jtulach@1334
|
1402 |
*/
|
jtulach@1334
|
1403 |
private final boolean subparse(String text, ParsePosition parsePosition,
|
jtulach@1334
|
1404 |
String positivePrefix, String negativePrefix,
|
jtulach@1334
|
1405 |
DigitList digits, boolean isExponent,
|
jtulach@1334
|
1406 |
boolean status[]) {
|
jtulach@1334
|
1407 |
int position = parsePosition.index;
|
jtulach@1334
|
1408 |
int oldStart = parsePosition.index;
|
jtulach@1334
|
1409 |
int backup;
|
jtulach@1334
|
1410 |
boolean gotPositive, gotNegative;
|
jtulach@1334
|
1411 |
|
jtulach@1334
|
1412 |
// check for positivePrefix; take longest
|
jtulach@1334
|
1413 |
gotPositive = text.regionMatches(position, positivePrefix, 0,
|
jtulach@1334
|
1414 |
positivePrefix.length());
|
jtulach@1334
|
1415 |
gotNegative = text.regionMatches(position, negativePrefix, 0,
|
jtulach@1334
|
1416 |
negativePrefix.length());
|
jtulach@1334
|
1417 |
|
jtulach@1334
|
1418 |
if (gotPositive && gotNegative) {
|
jtulach@1334
|
1419 |
if (positivePrefix.length() > negativePrefix.length()) {
|
jtulach@1334
|
1420 |
gotNegative = false;
|
jtulach@1334
|
1421 |
} else if (positivePrefix.length() < negativePrefix.length()) {
|
jtulach@1334
|
1422 |
gotPositive = false;
|
jtulach@1334
|
1423 |
}
|
jtulach@1334
|
1424 |
}
|
jtulach@1334
|
1425 |
|
jtulach@1334
|
1426 |
if (gotPositive) {
|
jtulach@1334
|
1427 |
position += positivePrefix.length();
|
jtulach@1334
|
1428 |
} else if (gotNegative) {
|
jtulach@1334
|
1429 |
position += negativePrefix.length();
|
jtulach@1334
|
1430 |
} else {
|
jtulach@1334
|
1431 |
parsePosition.errorIndex = position;
|
jtulach@1334
|
1432 |
return false;
|
jtulach@1334
|
1433 |
}
|
jtulach@1334
|
1434 |
|
jtulach@1334
|
1435 |
// process digits or Inf, find decimal position
|
jtulach@1334
|
1436 |
status[STATUS_INFINITE] = false;
|
jtulach@1334
|
1437 |
if (!isExponent && text.regionMatches(position,symbols.getInfinity(),0,
|
jtulach@1334
|
1438 |
symbols.getInfinity().length())) {
|
jtulach@1334
|
1439 |
position += symbols.getInfinity().length();
|
jtulach@1334
|
1440 |
status[STATUS_INFINITE] = true;
|
jtulach@1334
|
1441 |
} else {
|
jtulach@1334
|
1442 |
// We now have a string of digits, possibly with grouping symbols,
|
jtulach@1334
|
1443 |
// and decimal points. We want to process these into a DigitList.
|
jtulach@1334
|
1444 |
// We don't want to put a bunch of leading zeros into the DigitList
|
jtulach@1334
|
1445 |
// though, so we keep track of the location of the decimal point,
|
jtulach@1334
|
1446 |
// put only significant digits into the DigitList, and adjust the
|
jtulach@1334
|
1447 |
// exponent as needed.
|
jtulach@1334
|
1448 |
|
jtulach@1334
|
1449 |
digits.decimalAt = digits.count = 0;
|
jtulach@1334
|
1450 |
char zero = symbols.getZeroDigit();
|
jtulach@1334
|
1451 |
char decimal = isCurrencyFormat ?
|
jtulach@1334
|
1452 |
symbols.getMonetaryDecimalSeparator() :
|
jtulach@1334
|
1453 |
symbols.getDecimalSeparator();
|
jtulach@1334
|
1454 |
char grouping = symbols.getGroupingSeparator();
|
jtulach@1334
|
1455 |
String exponentString = symbols.getExponentSeparator();
|
jtulach@1334
|
1456 |
boolean sawDecimal = false;
|
jtulach@1334
|
1457 |
boolean sawExponent = false;
|
jtulach@1334
|
1458 |
boolean sawDigit = false;
|
jtulach@1334
|
1459 |
int exponent = 0; // Set to the exponent value, if any
|
jtulach@1334
|
1460 |
|
jtulach@1334
|
1461 |
// We have to track digitCount ourselves, because digits.count will
|
jtulach@1334
|
1462 |
// pin when the maximum allowable digits is reached.
|
jtulach@1334
|
1463 |
int digitCount = 0;
|
jtulach@1334
|
1464 |
|
jtulach@1334
|
1465 |
backup = -1;
|
jtulach@1334
|
1466 |
for (; position < text.length(); ++position) {
|
jtulach@1334
|
1467 |
char ch = text.charAt(position);
|
jtulach@1334
|
1468 |
|
jtulach@1334
|
1469 |
/* We recognize all digit ranges, not only the Latin digit range
|
jtulach@1334
|
1470 |
* '0'..'9'. We do so by using the Character.digit() method,
|
jtulach@1334
|
1471 |
* which converts a valid Unicode digit to the range 0..9.
|
jtulach@1334
|
1472 |
*
|
jtulach@1334
|
1473 |
* The character 'ch' may be a digit. If so, place its value
|
jtulach@1334
|
1474 |
* from 0 to 9 in 'digit'. First try using the locale digit,
|
jtulach@1334
|
1475 |
* which may or MAY NOT be a standard Unicode digit range. If
|
jtulach@1334
|
1476 |
* this fails, try using the standard Unicode digit ranges by
|
jtulach@1334
|
1477 |
* calling Character.digit(). If this also fails, digit will
|
jtulach@1334
|
1478 |
* have a value outside the range 0..9.
|
jtulach@1334
|
1479 |
*/
|
jtulach@1334
|
1480 |
int digit = ch - zero;
|
jtulach@1334
|
1481 |
if (digit < 0 || digit > 9) {
|
jtulach@1334
|
1482 |
digit = Character.digit(ch, 10);
|
jtulach@1334
|
1483 |
}
|
jtulach@1334
|
1484 |
|
jtulach@1334
|
1485 |
if (digit == 0) {
|
jtulach@1334
|
1486 |
// Cancel out backup setting (see grouping handler below)
|
jtulach@1334
|
1487 |
backup = -1; // Do this BEFORE continue statement below!!!
|
jtulach@1334
|
1488 |
sawDigit = true;
|
jtulach@1334
|
1489 |
|
jtulach@1334
|
1490 |
// Handle leading zeros
|
jtulach@1334
|
1491 |
if (digits.count == 0) {
|
jtulach@1334
|
1492 |
// Ignore leading zeros in integer part of number.
|
jtulach@1334
|
1493 |
if (!sawDecimal) {
|
jtulach@1334
|
1494 |
continue;
|
jtulach@1334
|
1495 |
}
|
jtulach@1334
|
1496 |
|
jtulach@1334
|
1497 |
// If we have seen the decimal, but no significant
|
jtulach@1334
|
1498 |
// digits yet, then we account for leading zeros by
|
jtulach@1334
|
1499 |
// decrementing the digits.decimalAt into negative
|
jtulach@1334
|
1500 |
// values.
|
jtulach@1334
|
1501 |
--digits.decimalAt;
|
jtulach@1334
|
1502 |
} else {
|
jtulach@1334
|
1503 |
++digitCount;
|
jtulach@1334
|
1504 |
digits.append((char)(digit + '0'));
|
jtulach@1334
|
1505 |
}
|
jtulach@1334
|
1506 |
} else if (digit > 0 && digit <= 9) { // [sic] digit==0 handled above
|
jtulach@1334
|
1507 |
sawDigit = true;
|
jtulach@1334
|
1508 |
++digitCount;
|
jtulach@1334
|
1509 |
digits.append((char)(digit + '0'));
|
jtulach@1334
|
1510 |
|
jtulach@1334
|
1511 |
// Cancel out backup setting (see grouping handler below)
|
jtulach@1334
|
1512 |
backup = -1;
|
jtulach@1334
|
1513 |
} else if (!isExponent && ch == decimal) {
|
jtulach@1334
|
1514 |
// If we're only parsing integers, or if we ALREADY saw the
|
jtulach@1334
|
1515 |
// decimal, then don't parse this one.
|
jtulach@1334
|
1516 |
if (isParseIntegerOnly() || sawDecimal) {
|
jtulach@1334
|
1517 |
break;
|
jtulach@1334
|
1518 |
}
|
jtulach@1334
|
1519 |
digits.decimalAt = digitCount; // Not digits.count!
|
jtulach@1334
|
1520 |
sawDecimal = true;
|
jtulach@1334
|
1521 |
} else if (!isExponent && ch == grouping && isGroupingUsed()) {
|
jtulach@1334
|
1522 |
if (sawDecimal) {
|
jtulach@1334
|
1523 |
break;
|
jtulach@1334
|
1524 |
}
|
jtulach@1334
|
1525 |
// Ignore grouping characters, if we are using them, but
|
jtulach@1334
|
1526 |
// require that they be followed by a digit. Otherwise
|
jtulach@1334
|
1527 |
// we backup and reprocess them.
|
jtulach@1334
|
1528 |
backup = position;
|
jtulach@1334
|
1529 |
} else if (!isExponent && text.regionMatches(position, exponentString, 0, exponentString.length())
|
jtulach@1334
|
1530 |
&& !sawExponent) {
|
jtulach@1334
|
1531 |
// Process the exponent by recursively calling this method.
|
jtulach@1334
|
1532 |
ParsePosition pos = new ParsePosition(position + exponentString.length());
|
jtulach@1334
|
1533 |
boolean[] stat = new boolean[STATUS_LENGTH];
|
jtulach@1334
|
1534 |
DigitList exponentDigits = new DigitList();
|
jtulach@1334
|
1535 |
|
jtulach@1334
|
1536 |
if (subparse(text, pos, "", Character.toString(symbols.getMinusSign()), exponentDigits, true, stat) &&
|
jtulach@1334
|
1537 |
exponentDigits.fitsIntoLong(stat[STATUS_POSITIVE], true)) {
|
jtulach@1334
|
1538 |
position = pos.index; // Advance past the exponent
|
jtulach@1334
|
1539 |
exponent = (int)exponentDigits.getLong();
|
jtulach@1334
|
1540 |
if (!stat[STATUS_POSITIVE]) {
|
jtulach@1334
|
1541 |
exponent = -exponent;
|
jtulach@1334
|
1542 |
}
|
jtulach@1334
|
1543 |
sawExponent = true;
|
jtulach@1334
|
1544 |
}
|
jtulach@1334
|
1545 |
break; // Whether we fail or succeed, we exit this loop
|
jtulach@1334
|
1546 |
}
|
jtulach@1334
|
1547 |
else {
|
jtulach@1334
|
1548 |
break;
|
jtulach@1334
|
1549 |
}
|
jtulach@1334
|
1550 |
}
|
jtulach@1334
|
1551 |
|
jtulach@1334
|
1552 |
if (backup != -1) {
|
jtulach@1334
|
1553 |
position = backup;
|
jtulach@1334
|
1554 |
}
|
jtulach@1334
|
1555 |
|
jtulach@1334
|
1556 |
// If there was no decimal point we have an integer
|
jtulach@1334
|
1557 |
if (!sawDecimal) {
|
jtulach@1334
|
1558 |
digits.decimalAt = digitCount; // Not digits.count!
|
jtulach@1334
|
1559 |
}
|
jtulach@1334
|
1560 |
|
jtulach@1334
|
1561 |
// Adjust for exponent, if any
|
jtulach@1334
|
1562 |
digits.decimalAt += exponent;
|
jtulach@1334
|
1563 |
|
jtulach@1334
|
1564 |
// If none of the text string was recognized. For example, parse
|
jtulach@1334
|
1565 |
// "x" with pattern "#0.00" (return index and error index both 0)
|
jtulach@1334
|
1566 |
// parse "$" with pattern "$#0.00". (return index 0 and error
|
jtulach@1334
|
1567 |
// index 1).
|
jtulach@1334
|
1568 |
if (!sawDigit && digitCount == 0) {
|
jtulach@1334
|
1569 |
parsePosition.index = oldStart;
|
jtulach@1334
|
1570 |
parsePosition.errorIndex = oldStart;
|
jtulach@1334
|
1571 |
return false;
|
jtulach@1334
|
1572 |
}
|
jtulach@1334
|
1573 |
}
|
jtulach@1334
|
1574 |
|
jtulach@1334
|
1575 |
// check for suffix
|
jtulach@1334
|
1576 |
if (!isExponent) {
|
jtulach@1334
|
1577 |
if (gotPositive) {
|
jtulach@1334
|
1578 |
gotPositive = text.regionMatches(position,positiveSuffix,0,
|
jtulach@1334
|
1579 |
positiveSuffix.length());
|
jtulach@1334
|
1580 |
}
|
jtulach@1334
|
1581 |
if (gotNegative) {
|
jtulach@1334
|
1582 |
gotNegative = text.regionMatches(position,negativeSuffix,0,
|
jtulach@1334
|
1583 |
negativeSuffix.length());
|
jtulach@1334
|
1584 |
}
|
jtulach@1334
|
1585 |
|
jtulach@1334
|
1586 |
// if both match, take longest
|
jtulach@1334
|
1587 |
if (gotPositive && gotNegative) {
|
jtulach@1334
|
1588 |
if (positiveSuffix.length() > negativeSuffix.length()) {
|
jtulach@1334
|
1589 |
gotNegative = false;
|
jtulach@1334
|
1590 |
} else if (positiveSuffix.length() < negativeSuffix.length()) {
|
jtulach@1334
|
1591 |
gotPositive = false;
|
jtulach@1334
|
1592 |
}
|
jtulach@1334
|
1593 |
}
|
jtulach@1334
|
1594 |
|
jtulach@1334
|
1595 |
// fail if neither or both
|
jtulach@1334
|
1596 |
if (gotPositive == gotNegative) {
|
jtulach@1334
|
1597 |
parsePosition.errorIndex = position;
|
jtulach@1334
|
1598 |
return false;
|
jtulach@1334
|
1599 |
}
|
jtulach@1334
|
1600 |
|
jtulach@1334
|
1601 |
parsePosition.index = position +
|
jtulach@1334
|
1602 |
(gotPositive ? positiveSuffix.length() : negativeSuffix.length()); // mark success!
|
jtulach@1334
|
1603 |
} else {
|
jtulach@1334
|
1604 |
parsePosition.index = position;
|
jtulach@1334
|
1605 |
}
|
jtulach@1334
|
1606 |
|
jtulach@1334
|
1607 |
status[STATUS_POSITIVE] = gotPositive;
|
jtulach@1334
|
1608 |
if (parsePosition.index == oldStart) {
|
jtulach@1334
|
1609 |
parsePosition.errorIndex = position;
|
jtulach@1334
|
1610 |
return false;
|
jtulach@1334
|
1611 |
}
|
jtulach@1334
|
1612 |
return true;
|
jtulach@1334
|
1613 |
}
|
jtulach@1334
|
1614 |
|
jtulach@1334
|
1615 |
/**
|
jtulach@1334
|
1616 |
* Returns a copy of the decimal format symbols, which is generally not
|
jtulach@1334
|
1617 |
* changed by the programmer or user.
|
jtulach@1334
|
1618 |
* @return a copy of the desired DecimalFormatSymbols
|
jtulach@1334
|
1619 |
* @see java.text.DecimalFormatSymbols
|
jtulach@1334
|
1620 |
*/
|
jtulach@1334
|
1621 |
public DecimalFormatSymbols getDecimalFormatSymbols() {
|
jtulach@1334
|
1622 |
try {
|
jtulach@1334
|
1623 |
// don't allow multiple references
|
jtulach@1334
|
1624 |
return (DecimalFormatSymbols) symbols.clone();
|
jtulach@1334
|
1625 |
} catch (Exception foo) {
|
jtulach@1334
|
1626 |
return null; // should never happen
|
jtulach@1334
|
1627 |
}
|
jtulach@1334
|
1628 |
}
|
jtulach@1334
|
1629 |
|
jtulach@1334
|
1630 |
|
jtulach@1334
|
1631 |
/**
|
jtulach@1334
|
1632 |
* Sets the decimal format symbols, which is generally not changed
|
jtulach@1334
|
1633 |
* by the programmer or user.
|
jtulach@1334
|
1634 |
* @param newSymbols desired DecimalFormatSymbols
|
jtulach@1334
|
1635 |
* @see java.text.DecimalFormatSymbols
|
jtulach@1334
|
1636 |
*/
|
jtulach@1334
|
1637 |
public void setDecimalFormatSymbols(DecimalFormatSymbols newSymbols) {
|
jtulach@1334
|
1638 |
try {
|
jtulach@1334
|
1639 |
// don't allow multiple references
|
jtulach@1334
|
1640 |
symbols = (DecimalFormatSymbols) newSymbols.clone();
|
jtulach@1334
|
1641 |
expandAffixes();
|
jtulach@1334
|
1642 |
} catch (Exception foo) {
|
jtulach@1334
|
1643 |
// should never happen
|
jtulach@1334
|
1644 |
}
|
jtulach@1334
|
1645 |
}
|
jtulach@1334
|
1646 |
|
jtulach@1334
|
1647 |
/**
|
jtulach@1334
|
1648 |
* Get the positive prefix.
|
jtulach@1334
|
1649 |
* <P>Examples: +123, $123, sFr123
|
jtulach@1334
|
1650 |
*/
|
jtulach@1334
|
1651 |
public String getPositivePrefix () {
|
jtulach@1334
|
1652 |
return positivePrefix;
|
jtulach@1334
|
1653 |
}
|
jtulach@1334
|
1654 |
|
jtulach@1334
|
1655 |
/**
|
jtulach@1334
|
1656 |
* Set the positive prefix.
|
jtulach@1334
|
1657 |
* <P>Examples: +123, $123, sFr123
|
jtulach@1334
|
1658 |
*/
|
jtulach@1334
|
1659 |
public void setPositivePrefix (String newValue) {
|
jtulach@1334
|
1660 |
positivePrefix = newValue;
|
jtulach@1334
|
1661 |
posPrefixPattern = null;
|
jtulach@1334
|
1662 |
positivePrefixFieldPositions = null;
|
jtulach@1334
|
1663 |
}
|
jtulach@1334
|
1664 |
|
jtulach@1334
|
1665 |
/**
|
jtulach@1334
|
1666 |
* Returns the FieldPositions of the fields in the prefix used for
|
jtulach@1334
|
1667 |
* positive numbers. This is not used if the user has explicitly set
|
jtulach@1334
|
1668 |
* a positive prefix via <code>setPositivePrefix</code>. This is
|
jtulach@1334
|
1669 |
* lazily created.
|
jtulach@1334
|
1670 |
*
|
jtulach@1334
|
1671 |
* @return FieldPositions in positive prefix
|
jtulach@1334
|
1672 |
*/
|
jtulach@1334
|
1673 |
private FieldPosition[] getPositivePrefixFieldPositions() {
|
jtulach@1334
|
1674 |
if (positivePrefixFieldPositions == null) {
|
jtulach@1334
|
1675 |
if (posPrefixPattern != null) {
|
jtulach@1334
|
1676 |
positivePrefixFieldPositions = expandAffix(posPrefixPattern);
|
jtulach@1334
|
1677 |
}
|
jtulach@1334
|
1678 |
else {
|
jtulach@1334
|
1679 |
positivePrefixFieldPositions = EmptyFieldPositionArray;
|
jtulach@1334
|
1680 |
}
|
jtulach@1334
|
1681 |
}
|
jtulach@1334
|
1682 |
return positivePrefixFieldPositions;
|
jtulach@1334
|
1683 |
}
|
jtulach@1334
|
1684 |
|
jtulach@1334
|
1685 |
/**
|
jtulach@1334
|
1686 |
* Get the negative prefix.
|
jtulach@1334
|
1687 |
* <P>Examples: -123, ($123) (with negative suffix), sFr-123
|
jtulach@1334
|
1688 |
*/
|
jtulach@1334
|
1689 |
public String getNegativePrefix () {
|
jtulach@1334
|
1690 |
return negativePrefix;
|
jtulach@1334
|
1691 |
}
|
jtulach@1334
|
1692 |
|
jtulach@1334
|
1693 |
/**
|
jtulach@1334
|
1694 |
* Set the negative prefix.
|
jtulach@1334
|
1695 |
* <P>Examples: -123, ($123) (with negative suffix), sFr-123
|
jtulach@1334
|
1696 |
*/
|
jtulach@1334
|
1697 |
public void setNegativePrefix (String newValue) {
|
jtulach@1334
|
1698 |
negativePrefix = newValue;
|
jtulach@1334
|
1699 |
negPrefixPattern = null;
|
jtulach@1334
|
1700 |
}
|
jtulach@1334
|
1701 |
|
jtulach@1334
|
1702 |
/**
|
jtulach@1334
|
1703 |
* Returns the FieldPositions of the fields in the prefix used for
|
jtulach@1334
|
1704 |
* negative numbers. This is not used if the user has explicitly set
|
jtulach@1334
|
1705 |
* a negative prefix via <code>setNegativePrefix</code>. This is
|
jtulach@1334
|
1706 |
* lazily created.
|
jtulach@1334
|
1707 |
*
|
jtulach@1334
|
1708 |
* @return FieldPositions in positive prefix
|
jtulach@1334
|
1709 |
*/
|
jtulach@1334
|
1710 |
private FieldPosition[] getNegativePrefixFieldPositions() {
|
jtulach@1334
|
1711 |
if (negativePrefixFieldPositions == null) {
|
jtulach@1334
|
1712 |
if (negPrefixPattern != null) {
|
jtulach@1334
|
1713 |
negativePrefixFieldPositions = expandAffix(negPrefixPattern);
|
jtulach@1334
|
1714 |
}
|
jtulach@1334
|
1715 |
else {
|
jtulach@1334
|
1716 |
negativePrefixFieldPositions = EmptyFieldPositionArray;
|
jtulach@1334
|
1717 |
}
|
jtulach@1334
|
1718 |
}
|
jtulach@1334
|
1719 |
return negativePrefixFieldPositions;
|
jtulach@1334
|
1720 |
}
|
jtulach@1334
|
1721 |
|
jtulach@1334
|
1722 |
/**
|
jtulach@1334
|
1723 |
* Get the positive suffix.
|
jtulach@1334
|
1724 |
* <P>Example: 123%
|
jtulach@1334
|
1725 |
*/
|
jtulach@1334
|
1726 |
public String getPositiveSuffix () {
|
jtulach@1334
|
1727 |
return positiveSuffix;
|
jtulach@1334
|
1728 |
}
|
jtulach@1334
|
1729 |
|
jtulach@1334
|
1730 |
/**
|
jtulach@1334
|
1731 |
* Set the positive suffix.
|
jtulach@1334
|
1732 |
* <P>Example: 123%
|
jtulach@1334
|
1733 |
*/
|
jtulach@1334
|
1734 |
public void setPositiveSuffix (String newValue) {
|
jtulach@1334
|
1735 |
positiveSuffix = newValue;
|
jtulach@1334
|
1736 |
posSuffixPattern = null;
|
jtulach@1334
|
1737 |
}
|
jtulach@1334
|
1738 |
|
jtulach@1334
|
1739 |
/**
|
jtulach@1334
|
1740 |
* Returns the FieldPositions of the fields in the suffix used for
|
jtulach@1334
|
1741 |
* positive numbers. This is not used if the user has explicitly set
|
jtulach@1334
|
1742 |
* a positive suffix via <code>setPositiveSuffix</code>. This is
|
jtulach@1334
|
1743 |
* lazily created.
|
jtulach@1334
|
1744 |
*
|
jtulach@1334
|
1745 |
* @return FieldPositions in positive prefix
|
jtulach@1334
|
1746 |
*/
|
jtulach@1334
|
1747 |
private FieldPosition[] getPositiveSuffixFieldPositions() {
|
jtulach@1334
|
1748 |
if (positiveSuffixFieldPositions == null) {
|
jtulach@1334
|
1749 |
if (posSuffixPattern != null) {
|
jtulach@1334
|
1750 |
positiveSuffixFieldPositions = expandAffix(posSuffixPattern);
|
jtulach@1334
|
1751 |
}
|
jtulach@1334
|
1752 |
else {
|
jtulach@1334
|
1753 |
positiveSuffixFieldPositions = EmptyFieldPositionArray;
|
jtulach@1334
|
1754 |
}
|
jtulach@1334
|
1755 |
}
|
jtulach@1334
|
1756 |
return positiveSuffixFieldPositions;
|
jtulach@1334
|
1757 |
}
|
jtulach@1334
|
1758 |
|
jtulach@1334
|
1759 |
/**
|
jtulach@1334
|
1760 |
* Get the negative suffix.
|
jtulach@1334
|
1761 |
* <P>Examples: -123%, ($123) (with positive suffixes)
|
jtulach@1334
|
1762 |
*/
|
jtulach@1334
|
1763 |
public String getNegativeSuffix () {
|
jtulach@1334
|
1764 |
return negativeSuffix;
|
jtulach@1334
|
1765 |
}
|
jtulach@1334
|
1766 |
|
jtulach@1334
|
1767 |
/**
|
jtulach@1334
|
1768 |
* Set the negative suffix.
|
jtulach@1334
|
1769 |
* <P>Examples: 123%
|
jtulach@1334
|
1770 |
*/
|
jtulach@1334
|
1771 |
public void setNegativeSuffix (String newValue) {
|
jtulach@1334
|
1772 |
negativeSuffix = newValue;
|
jtulach@1334
|
1773 |
negSuffixPattern = null;
|
jtulach@1334
|
1774 |
}
|
jtulach@1334
|
1775 |
|
jtulach@1334
|
1776 |
/**
|
jtulach@1334
|
1777 |
* Returns the FieldPositions of the fields in the suffix used for
|
jtulach@1334
|
1778 |
* negative numbers. This is not used if the user has explicitly set
|
jtulach@1334
|
1779 |
* a negative suffix via <code>setNegativeSuffix</code>. This is
|
jtulach@1334
|
1780 |
* lazily created.
|
jtulach@1334
|
1781 |
*
|
jtulach@1334
|
1782 |
* @return FieldPositions in positive prefix
|
jtulach@1334
|
1783 |
*/
|
jtulach@1334
|
1784 |
private FieldPosition[] getNegativeSuffixFieldPositions() {
|
jtulach@1334
|
1785 |
if (negativeSuffixFieldPositions == null) {
|
jtulach@1334
|
1786 |
if (negSuffixPattern != null) {
|
jtulach@1334
|
1787 |
negativeSuffixFieldPositions = expandAffix(negSuffixPattern);
|
jtulach@1334
|
1788 |
}
|
jtulach@1334
|
1789 |
else {
|
jtulach@1334
|
1790 |
negativeSuffixFieldPositions = EmptyFieldPositionArray;
|
jtulach@1334
|
1791 |
}
|
jtulach@1334
|
1792 |
}
|
jtulach@1334
|
1793 |
return negativeSuffixFieldPositions;
|
jtulach@1334
|
1794 |
}
|
jtulach@1334
|
1795 |
|
jtulach@1334
|
1796 |
/**
|
jtulach@1334
|
1797 |
* Gets the multiplier for use in percent, per mille, and similar
|
jtulach@1334
|
1798 |
* formats.
|
jtulach@1334
|
1799 |
*
|
jtulach@1334
|
1800 |
* @see #setMultiplier(int)
|
jtulach@1334
|
1801 |
*/
|
jtulach@1334
|
1802 |
public int getMultiplier () {
|
jtulach@1334
|
1803 |
return multiplier;
|
jtulach@1334
|
1804 |
}
|
jtulach@1334
|
1805 |
|
jtulach@1334
|
1806 |
/**
|
jtulach@1334
|
1807 |
* Sets the multiplier for use in percent, per mille, and similar
|
jtulach@1334
|
1808 |
* formats.
|
jtulach@1334
|
1809 |
* For a percent format, set the multiplier to 100 and the suffixes to
|
jtulach@1334
|
1810 |
* have '%' (for Arabic, use the Arabic percent sign).
|
jtulach@1334
|
1811 |
* For a per mille format, set the multiplier to 1000 and the suffixes to
|
jtulach@1334
|
1812 |
* have '\u2030'.
|
jtulach@1334
|
1813 |
*
|
jtulach@1334
|
1814 |
* <P>Example: with multiplier 100, 1.23 is formatted as "123", and
|
jtulach@1334
|
1815 |
* "123" is parsed into 1.23.
|
jtulach@1334
|
1816 |
*
|
jtulach@1334
|
1817 |
* @see #getMultiplier
|
jtulach@1334
|
1818 |
*/
|
jtulach@1334
|
1819 |
public void setMultiplier (int newValue) {
|
jtulach@1334
|
1820 |
multiplier = newValue;
|
jtulach@1334
|
1821 |
bigDecimalMultiplier = null;
|
jtulach@1334
|
1822 |
bigIntegerMultiplier = null;
|
jtulach@1334
|
1823 |
}
|
jtulach@1334
|
1824 |
|
jtulach@1334
|
1825 |
/**
|
jtulach@1334
|
1826 |
* Return the grouping size. Grouping size is the number of digits between
|
jtulach@1334
|
1827 |
* grouping separators in the integer portion of a number. For example,
|
jtulach@1334
|
1828 |
* in the number "123,456.78", the grouping size is 3.
|
jtulach@1334
|
1829 |
* @see #setGroupingSize
|
jtulach@1334
|
1830 |
* @see java.text.NumberFormat#isGroupingUsed
|
jtulach@1334
|
1831 |
* @see java.text.DecimalFormatSymbols#getGroupingSeparator
|
jtulach@1334
|
1832 |
*/
|
jtulach@1334
|
1833 |
public int getGroupingSize () {
|
jtulach@1334
|
1834 |
return groupingSize;
|
jtulach@1334
|
1835 |
}
|
jtulach@1334
|
1836 |
|
jtulach@1334
|
1837 |
/**
|
jtulach@1334
|
1838 |
* Set the grouping size. Grouping size is the number of digits between
|
jtulach@1334
|
1839 |
* grouping separators in the integer portion of a number. For example,
|
jtulach@1334
|
1840 |
* in the number "123,456.78", the grouping size is 3.
|
jtulach@1334
|
1841 |
* <br>
|
jtulach@1334
|
1842 |
* The value passed in is converted to a byte, which may lose information.
|
jtulach@1334
|
1843 |
* @see #getGroupingSize
|
jtulach@1334
|
1844 |
* @see java.text.NumberFormat#setGroupingUsed
|
jtulach@1334
|
1845 |
* @see java.text.DecimalFormatSymbols#setGroupingSeparator
|
jtulach@1334
|
1846 |
*/
|
jtulach@1334
|
1847 |
public void setGroupingSize (int newValue) {
|
jtulach@1334
|
1848 |
groupingSize = (byte)newValue;
|
jtulach@1334
|
1849 |
}
|
jtulach@1334
|
1850 |
|
jtulach@1334
|
1851 |
/**
|
jtulach@1334
|
1852 |
* Allows you to get the behavior of the decimal separator with integers.
|
jtulach@1334
|
1853 |
* (The decimal separator will always appear with decimals.)
|
jtulach@1334
|
1854 |
* <P>Example: Decimal ON: 12345 -> 12345.; OFF: 12345 -> 12345
|
jtulach@1334
|
1855 |
*/
|
jtulach@1334
|
1856 |
public boolean isDecimalSeparatorAlwaysShown() {
|
jtulach@1334
|
1857 |
return decimalSeparatorAlwaysShown;
|
jtulach@1334
|
1858 |
}
|
jtulach@1334
|
1859 |
|
jtulach@1334
|
1860 |
/**
|
jtulach@1334
|
1861 |
* Allows you to set the behavior of the decimal separator with integers.
|
jtulach@1334
|
1862 |
* (The decimal separator will always appear with decimals.)
|
jtulach@1334
|
1863 |
* <P>Example: Decimal ON: 12345 -> 12345.; OFF: 12345 -> 12345
|
jtulach@1334
|
1864 |
*/
|
jtulach@1334
|
1865 |
public void setDecimalSeparatorAlwaysShown(boolean newValue) {
|
jtulach@1334
|
1866 |
decimalSeparatorAlwaysShown = newValue;
|
jtulach@1334
|
1867 |
}
|
jtulach@1334
|
1868 |
|
jtulach@1334
|
1869 |
/**
|
jtulach@1334
|
1870 |
* Returns whether the {@link #parse(java.lang.String, java.text.ParsePosition)}
|
jtulach@1334
|
1871 |
* method returns <code>BigDecimal</code>. The default value is false.
|
jtulach@1334
|
1872 |
* @see #setParseBigDecimal
|
jtulach@1334
|
1873 |
* @since 1.5
|
jtulach@1334
|
1874 |
*/
|
jtulach@1334
|
1875 |
public boolean isParseBigDecimal() {
|
jtulach@1334
|
1876 |
return parseBigDecimal;
|
jtulach@1334
|
1877 |
}
|
jtulach@1334
|
1878 |
|
jtulach@1334
|
1879 |
/**
|
jtulach@1334
|
1880 |
* Sets whether the {@link #parse(java.lang.String, java.text.ParsePosition)}
|
jtulach@1334
|
1881 |
* method returns <code>BigDecimal</code>.
|
jtulach@1334
|
1882 |
* @see #isParseBigDecimal
|
jtulach@1334
|
1883 |
* @since 1.5
|
jtulach@1334
|
1884 |
*/
|
jtulach@1334
|
1885 |
public void setParseBigDecimal(boolean newValue) {
|
jtulach@1334
|
1886 |
parseBigDecimal = newValue;
|
jtulach@1334
|
1887 |
}
|
jtulach@1334
|
1888 |
|
jtulach@1334
|
1889 |
/**
|
jtulach@1334
|
1890 |
* Standard override; no change in semantics.
|
jtulach@1334
|
1891 |
*/
|
jtulach@1334
|
1892 |
public Object clone() {
|
jtulach@1334
|
1893 |
try {
|
jtulach@1334
|
1894 |
DecimalFormat other = (DecimalFormat) super.clone();
|
jtulach@1334
|
1895 |
other.symbols = (DecimalFormatSymbols) symbols.clone();
|
jtulach@1334
|
1896 |
other.digitList = (DigitList) digitList.clone();
|
jtulach@1334
|
1897 |
return other;
|
jtulach@1334
|
1898 |
} catch (Exception e) {
|
jtulach@1334
|
1899 |
throw new InternalError();
|
jtulach@1334
|
1900 |
}
|
jtulach@1334
|
1901 |
}
|
jtulach@1334
|
1902 |
|
jtulach@1334
|
1903 |
/**
|
jtulach@1334
|
1904 |
* Overrides equals
|
jtulach@1334
|
1905 |
*/
|
jtulach@1334
|
1906 |
public boolean equals(Object obj)
|
jtulach@1334
|
1907 |
{
|
jtulach@1334
|
1908 |
if (obj == null) return false;
|
jtulach@1334
|
1909 |
if (!super.equals(obj)) return false; // super does class check
|
jtulach@1334
|
1910 |
DecimalFormat other = (DecimalFormat) obj;
|
jtulach@1334
|
1911 |
return ((posPrefixPattern == other.posPrefixPattern &&
|
jtulach@1334
|
1912 |
positivePrefix.equals(other.positivePrefix))
|
jtulach@1334
|
1913 |
|| (posPrefixPattern != null &&
|
jtulach@1334
|
1914 |
posPrefixPattern.equals(other.posPrefixPattern)))
|
jtulach@1334
|
1915 |
&& ((posSuffixPattern == other.posSuffixPattern &&
|
jtulach@1334
|
1916 |
positiveSuffix.equals(other.positiveSuffix))
|
jtulach@1334
|
1917 |
|| (posSuffixPattern != null &&
|
jtulach@1334
|
1918 |
posSuffixPattern.equals(other.posSuffixPattern)))
|
jtulach@1334
|
1919 |
&& ((negPrefixPattern == other.negPrefixPattern &&
|
jtulach@1334
|
1920 |
negativePrefix.equals(other.negativePrefix))
|
jtulach@1334
|
1921 |
|| (negPrefixPattern != null &&
|
jtulach@1334
|
1922 |
negPrefixPattern.equals(other.negPrefixPattern)))
|
jtulach@1334
|
1923 |
&& ((negSuffixPattern == other.negSuffixPattern &&
|
jtulach@1334
|
1924 |
negativeSuffix.equals(other.negativeSuffix))
|
jtulach@1334
|
1925 |
|| (negSuffixPattern != null &&
|
jtulach@1334
|
1926 |
negSuffixPattern.equals(other.negSuffixPattern)))
|
jtulach@1334
|
1927 |
&& multiplier == other.multiplier
|
jtulach@1334
|
1928 |
&& groupingSize == other.groupingSize
|
jtulach@1334
|
1929 |
&& decimalSeparatorAlwaysShown == other.decimalSeparatorAlwaysShown
|
jtulach@1334
|
1930 |
&& parseBigDecimal == other.parseBigDecimal
|
jtulach@1334
|
1931 |
&& useExponentialNotation == other.useExponentialNotation
|
jtulach@1334
|
1932 |
&& (!useExponentialNotation ||
|
jtulach@1334
|
1933 |
minExponentDigits == other.minExponentDigits)
|
jtulach@1334
|
1934 |
&& maximumIntegerDigits == other.maximumIntegerDigits
|
jtulach@1334
|
1935 |
&& minimumIntegerDigits == other.minimumIntegerDigits
|
jtulach@1334
|
1936 |
&& maximumFractionDigits == other.maximumFractionDigits
|
jtulach@1334
|
1937 |
&& minimumFractionDigits == other.minimumFractionDigits
|
jtulach@1334
|
1938 |
&& roundingMode == other.roundingMode
|
jtulach@1334
|
1939 |
&& symbols.equals(other.symbols);
|
jtulach@1334
|
1940 |
}
|
jtulach@1334
|
1941 |
|
jtulach@1334
|
1942 |
/**
|
jtulach@1334
|
1943 |
* Overrides hashCode
|
jtulach@1334
|
1944 |
*/
|
jtulach@1334
|
1945 |
public int hashCode() {
|
jtulach@1334
|
1946 |
return super.hashCode() * 37 + positivePrefix.hashCode();
|
jtulach@1334
|
1947 |
// just enough fields for a reasonable distribution
|
jtulach@1334
|
1948 |
}
|
jtulach@1334
|
1949 |
|
jtulach@1334
|
1950 |
/**
|
jtulach@1334
|
1951 |
* Synthesizes a pattern string that represents the current state
|
jtulach@1334
|
1952 |
* of this Format object.
|
jtulach@1334
|
1953 |
* @see #applyPattern
|
jtulach@1334
|
1954 |
*/
|
jtulach@1334
|
1955 |
public String toPattern() {
|
jtulach@1334
|
1956 |
return toPattern( false );
|
jtulach@1334
|
1957 |
}
|
jtulach@1334
|
1958 |
|
jtulach@1334
|
1959 |
/**
|
jtulach@1334
|
1960 |
* Synthesizes a localized pattern string that represents the current
|
jtulach@1334
|
1961 |
* state of this Format object.
|
jtulach@1334
|
1962 |
* @see #applyPattern
|
jtulach@1334
|
1963 |
*/
|
jtulach@1334
|
1964 |
public String toLocalizedPattern() {
|
jtulach@1334
|
1965 |
return toPattern( true );
|
jtulach@1334
|
1966 |
}
|
jtulach@1334
|
1967 |
|
jtulach@1334
|
1968 |
/**
|
jtulach@1334
|
1969 |
* Expand the affix pattern strings into the expanded affix strings. If any
|
jtulach@1334
|
1970 |
* affix pattern string is null, do not expand it. This method should be
|
jtulach@1334
|
1971 |
* called any time the symbols or the affix patterns change in order to keep
|
jtulach@1334
|
1972 |
* the expanded affix strings up to date.
|
jtulach@1334
|
1973 |
*/
|
jtulach@1334
|
1974 |
private void expandAffixes() {
|
jtulach@1334
|
1975 |
// Reuse one StringBuffer for better performance
|
jtulach@1334
|
1976 |
StringBuffer buffer = new StringBuffer();
|
jtulach@1334
|
1977 |
if (posPrefixPattern != null) {
|
jtulach@1334
|
1978 |
positivePrefix = expandAffix(posPrefixPattern, buffer);
|
jtulach@1334
|
1979 |
positivePrefixFieldPositions = null;
|
jtulach@1334
|
1980 |
}
|
jtulach@1334
|
1981 |
if (posSuffixPattern != null) {
|
jtulach@1334
|
1982 |
positiveSuffix = expandAffix(posSuffixPattern, buffer);
|
jtulach@1334
|
1983 |
positiveSuffixFieldPositions = null;
|
jtulach@1334
|
1984 |
}
|
jtulach@1334
|
1985 |
if (negPrefixPattern != null) {
|
jtulach@1334
|
1986 |
negativePrefix = expandAffix(negPrefixPattern, buffer);
|
jtulach@1334
|
1987 |
negativePrefixFieldPositions = null;
|
jtulach@1334
|
1988 |
}
|
jtulach@1334
|
1989 |
if (negSuffixPattern != null) {
|
jtulach@1334
|
1990 |
negativeSuffix = expandAffix(negSuffixPattern, buffer);
|
jtulach@1334
|
1991 |
negativeSuffixFieldPositions = null;
|
jtulach@1334
|
1992 |
}
|
jtulach@1334
|
1993 |
}
|
jtulach@1334
|
1994 |
|
jtulach@1334
|
1995 |
/**
|
jtulach@1334
|
1996 |
* Expand an affix pattern into an affix string. All characters in the
|
jtulach@1334
|
1997 |
* pattern are literal unless prefixed by QUOTE. The following characters
|
jtulach@1334
|
1998 |
* after QUOTE are recognized: PATTERN_PERCENT, PATTERN_PER_MILLE,
|
jtulach@1334
|
1999 |
* PATTERN_MINUS, and CURRENCY_SIGN. If CURRENCY_SIGN is doubled (QUOTE +
|
jtulach@1334
|
2000 |
* CURRENCY_SIGN + CURRENCY_SIGN), it is interpreted as an ISO 4217
|
jtulach@1334
|
2001 |
* currency code. Any other character after a QUOTE represents itself.
|
jtulach@1334
|
2002 |
* QUOTE must be followed by another character; QUOTE may not occur by
|
jtulach@1334
|
2003 |
* itself at the end of the pattern.
|
jtulach@1334
|
2004 |
*
|
jtulach@1334
|
2005 |
* @param pattern the non-null, possibly empty pattern
|
jtulach@1334
|
2006 |
* @param buffer a scratch StringBuffer; its contents will be lost
|
jtulach@1334
|
2007 |
* @return the expanded equivalent of pattern
|
jtulach@1334
|
2008 |
*/
|
jtulach@1334
|
2009 |
private String expandAffix(String pattern, StringBuffer buffer) {
|
jtulach@1334
|
2010 |
buffer.setLength(0);
|
jtulach@1334
|
2011 |
for (int i=0; i<pattern.length(); ) {
|
jtulach@1334
|
2012 |
char c = pattern.charAt(i++);
|
jtulach@1334
|
2013 |
if (c == QUOTE) {
|
jtulach@1334
|
2014 |
c = pattern.charAt(i++);
|
jtulach@1334
|
2015 |
switch (c) {
|
jtulach@1334
|
2016 |
case CURRENCY_SIGN:
|
jtulach@1334
|
2017 |
if (i<pattern.length() &&
|
jtulach@1334
|
2018 |
pattern.charAt(i) == CURRENCY_SIGN) {
|
jtulach@1334
|
2019 |
++i;
|
jtulach@1334
|
2020 |
buffer.append(symbols.getInternationalCurrencySymbol());
|
jtulach@1334
|
2021 |
} else {
|
jtulach@1334
|
2022 |
buffer.append(symbols.getCurrencySymbol());
|
jtulach@1334
|
2023 |
}
|
jtulach@1334
|
2024 |
continue;
|
jtulach@1334
|
2025 |
case PATTERN_PERCENT:
|
jtulach@1334
|
2026 |
c = symbols.getPercent();
|
jtulach@1334
|
2027 |
break;
|
jtulach@1334
|
2028 |
case PATTERN_PER_MILLE:
|
jtulach@1334
|
2029 |
c = symbols.getPerMill();
|
jtulach@1334
|
2030 |
break;
|
jtulach@1334
|
2031 |
case PATTERN_MINUS:
|
jtulach@1334
|
2032 |
c = symbols.getMinusSign();
|
jtulach@1334
|
2033 |
break;
|
jtulach@1334
|
2034 |
}
|
jtulach@1334
|
2035 |
}
|
jtulach@1334
|
2036 |
buffer.append(c);
|
jtulach@1334
|
2037 |
}
|
jtulach@1334
|
2038 |
return buffer.toString();
|
jtulach@1334
|
2039 |
}
|
jtulach@1334
|
2040 |
|
jtulach@1334
|
2041 |
/**
|
jtulach@1334
|
2042 |
* Expand an affix pattern into an array of FieldPositions describing
|
jtulach@1334
|
2043 |
* how the pattern would be expanded.
|
jtulach@1334
|
2044 |
* All characters in the
|
jtulach@1334
|
2045 |
* pattern are literal unless prefixed by QUOTE. The following characters
|
jtulach@1334
|
2046 |
* after QUOTE are recognized: PATTERN_PERCENT, PATTERN_PER_MILLE,
|
jtulach@1334
|
2047 |
* PATTERN_MINUS, and CURRENCY_SIGN. If CURRENCY_SIGN is doubled (QUOTE +
|
jtulach@1334
|
2048 |
* CURRENCY_SIGN + CURRENCY_SIGN), it is interpreted as an ISO 4217
|
jtulach@1334
|
2049 |
* currency code. Any other character after a QUOTE represents itself.
|
jtulach@1334
|
2050 |
* QUOTE must be followed by another character; QUOTE may not occur by
|
jtulach@1334
|
2051 |
* itself at the end of the pattern.
|
jtulach@1334
|
2052 |
*
|
jtulach@1334
|
2053 |
* @param pattern the non-null, possibly empty pattern
|
jtulach@1334
|
2054 |
* @return FieldPosition array of the resulting fields.
|
jtulach@1334
|
2055 |
*/
|
jtulach@1334
|
2056 |
private FieldPosition[] expandAffix(String pattern) {
|
jtulach@1334
|
2057 |
ArrayList positions = null;
|
jtulach@1334
|
2058 |
int stringIndex = 0;
|
jtulach@1334
|
2059 |
for (int i=0; i<pattern.length(); ) {
|
jtulach@1334
|
2060 |
char c = pattern.charAt(i++);
|
jtulach@1334
|
2061 |
if (c == QUOTE) {
|
jtulach@1334
|
2062 |
int field = -1;
|
jtulach@1334
|
2063 |
Format.Field fieldID = null;
|
jtulach@1334
|
2064 |
c = pattern.charAt(i++);
|
jtulach@1334
|
2065 |
switch (c) {
|
jtulach@1334
|
2066 |
case CURRENCY_SIGN:
|
jtulach@1334
|
2067 |
String string;
|
jtulach@1334
|
2068 |
if (i<pattern.length() &&
|
jtulach@1334
|
2069 |
pattern.charAt(i) == CURRENCY_SIGN) {
|
jtulach@1334
|
2070 |
++i;
|
jtulach@1334
|
2071 |
string = symbols.getInternationalCurrencySymbol();
|
jtulach@1334
|
2072 |
} else {
|
jtulach@1334
|
2073 |
string = symbols.getCurrencySymbol();
|
jtulach@1334
|
2074 |
}
|
jtulach@1334
|
2075 |
if (string.length() > 0) {
|
jtulach@1334
|
2076 |
if (positions == null) {
|
jtulach@1334
|
2077 |
positions = new ArrayList(2);
|
jtulach@1334
|
2078 |
}
|
jtulach@1334
|
2079 |
FieldPosition fp = new FieldPosition(Field.CURRENCY);
|
jtulach@1334
|
2080 |
fp.setBeginIndex(stringIndex);
|
jtulach@1334
|
2081 |
fp.setEndIndex(stringIndex + string.length());
|
jtulach@1334
|
2082 |
positions.add(fp);
|
jtulach@1334
|
2083 |
stringIndex += string.length();
|
jtulach@1334
|
2084 |
}
|
jtulach@1334
|
2085 |
continue;
|
jtulach@1334
|
2086 |
case PATTERN_PERCENT:
|
jtulach@1334
|
2087 |
c = symbols.getPercent();
|
jtulach@1334
|
2088 |
field = -1;
|
jtulach@1334
|
2089 |
fieldID = Field.PERCENT;
|
jtulach@1334
|
2090 |
break;
|
jtulach@1334
|
2091 |
case PATTERN_PER_MILLE:
|
jtulach@1334
|
2092 |
c = symbols.getPerMill();
|
jtulach@1334
|
2093 |
field = -1;
|
jtulach@1334
|
2094 |
fieldID = Field.PERMILLE;
|
jtulach@1334
|
2095 |
break;
|
jtulach@1334
|
2096 |
case PATTERN_MINUS:
|
jtulach@1334
|
2097 |
c = symbols.getMinusSign();
|
jtulach@1334
|
2098 |
field = -1;
|
jtulach@1334
|
2099 |
fieldID = Field.SIGN;
|
jtulach@1334
|
2100 |
break;
|
jtulach@1334
|
2101 |
}
|
jtulach@1334
|
2102 |
if (fieldID != null) {
|
jtulach@1334
|
2103 |
if (positions == null) {
|
jtulach@1334
|
2104 |
positions = new ArrayList(2);
|
jtulach@1334
|
2105 |
}
|
jtulach@1334
|
2106 |
FieldPosition fp = new FieldPosition(fieldID, field);
|
jtulach@1334
|
2107 |
fp.setBeginIndex(stringIndex);
|
jtulach@1334
|
2108 |
fp.setEndIndex(stringIndex + 1);
|
jtulach@1334
|
2109 |
positions.add(fp);
|
jtulach@1334
|
2110 |
}
|
jtulach@1334
|
2111 |
}
|
jtulach@1334
|
2112 |
stringIndex++;
|
jtulach@1334
|
2113 |
}
|
jtulach@1334
|
2114 |
if (positions != null) {
|
jtulach@1334
|
2115 |
return (FieldPosition[])positions.toArray(EmptyFieldPositionArray);
|
jtulach@1334
|
2116 |
}
|
jtulach@1334
|
2117 |
return EmptyFieldPositionArray;
|
jtulach@1334
|
2118 |
}
|
jtulach@1334
|
2119 |
|
jtulach@1334
|
2120 |
/**
|
jtulach@1334
|
2121 |
* Appends an affix pattern to the given StringBuffer, quoting special
|
jtulach@1334
|
2122 |
* characters as needed. Uses the internal affix pattern, if that exists,
|
jtulach@1334
|
2123 |
* or the literal affix, if the internal affix pattern is null. The
|
jtulach@1334
|
2124 |
* appended string will generate the same affix pattern (or literal affix)
|
jtulach@1334
|
2125 |
* when passed to toPattern().
|
jtulach@1334
|
2126 |
*
|
jtulach@1334
|
2127 |
* @param buffer the affix string is appended to this
|
jtulach@1334
|
2128 |
* @param affixPattern a pattern such as posPrefixPattern; may be null
|
jtulach@1334
|
2129 |
* @param expAffix a corresponding expanded affix, such as positivePrefix.
|
jtulach@1334
|
2130 |
* Ignored unless affixPattern is null. If affixPattern is null, then
|
jtulach@1334
|
2131 |
* expAffix is appended as a literal affix.
|
jtulach@1334
|
2132 |
* @param localized true if the appended pattern should contain localized
|
jtulach@1334
|
2133 |
* pattern characters; otherwise, non-localized pattern chars are appended
|
jtulach@1334
|
2134 |
*/
|
jtulach@1334
|
2135 |
private void appendAffix(StringBuffer buffer, String affixPattern,
|
jtulach@1334
|
2136 |
String expAffix, boolean localized) {
|
jtulach@1334
|
2137 |
if (affixPattern == null) {
|
jtulach@1334
|
2138 |
appendAffix(buffer, expAffix, localized);
|
jtulach@1334
|
2139 |
} else {
|
jtulach@1334
|
2140 |
int i;
|
jtulach@1334
|
2141 |
for (int pos=0; pos<affixPattern.length(); pos=i) {
|
jtulach@1334
|
2142 |
i = affixPattern.indexOf(QUOTE, pos);
|
jtulach@1334
|
2143 |
if (i < 0) {
|
jtulach@1334
|
2144 |
appendAffix(buffer, affixPattern.substring(pos), localized);
|
jtulach@1334
|
2145 |
break;
|
jtulach@1334
|
2146 |
}
|
jtulach@1334
|
2147 |
if (i > pos) {
|
jtulach@1334
|
2148 |
appendAffix(buffer, affixPattern.substring(pos, i), localized);
|
jtulach@1334
|
2149 |
}
|
jtulach@1334
|
2150 |
char c = affixPattern.charAt(++i);
|
jtulach@1334
|
2151 |
++i;
|
jtulach@1334
|
2152 |
if (c == QUOTE) {
|
jtulach@1334
|
2153 |
buffer.append(c);
|
jtulach@1334
|
2154 |
// Fall through and append another QUOTE below
|
jtulach@1334
|
2155 |
} else if (c == CURRENCY_SIGN &&
|
jtulach@1334
|
2156 |
i<affixPattern.length() &&
|
jtulach@1334
|
2157 |
affixPattern.charAt(i) == CURRENCY_SIGN) {
|
jtulach@1334
|
2158 |
++i;
|
jtulach@1334
|
2159 |
buffer.append(c);
|
jtulach@1334
|
2160 |
// Fall through and append another CURRENCY_SIGN below
|
jtulach@1334
|
2161 |
} else if (localized) {
|
jtulach@1334
|
2162 |
switch (c) {
|
jtulach@1334
|
2163 |
case PATTERN_PERCENT:
|
jtulach@1334
|
2164 |
c = symbols.getPercent();
|
jtulach@1334
|
2165 |
break;
|
jtulach@1334
|
2166 |
case PATTERN_PER_MILLE:
|
jtulach@1334
|
2167 |
c = symbols.getPerMill();
|
jtulach@1334
|
2168 |
break;
|
jtulach@1334
|
2169 |
case PATTERN_MINUS:
|
jtulach@1334
|
2170 |
c = symbols.getMinusSign();
|
jtulach@1334
|
2171 |
break;
|
jtulach@1334
|
2172 |
}
|
jtulach@1334
|
2173 |
}
|
jtulach@1334
|
2174 |
buffer.append(c);
|
jtulach@1334
|
2175 |
}
|
jtulach@1334
|
2176 |
}
|
jtulach@1334
|
2177 |
}
|
jtulach@1334
|
2178 |
|
jtulach@1334
|
2179 |
/**
|
jtulach@1334
|
2180 |
* Append an affix to the given StringBuffer, using quotes if
|
jtulach@1334
|
2181 |
* there are special characters. Single quotes themselves must be
|
jtulach@1334
|
2182 |
* escaped in either case.
|
jtulach@1334
|
2183 |
*/
|
jtulach@1334
|
2184 |
private void appendAffix(StringBuffer buffer, String affix, boolean localized) {
|
jtulach@1334
|
2185 |
boolean needQuote;
|
jtulach@1334
|
2186 |
if (localized) {
|
jtulach@1334
|
2187 |
needQuote = affix.indexOf(symbols.getZeroDigit()) >= 0
|
jtulach@1334
|
2188 |
|| affix.indexOf(symbols.getGroupingSeparator()) >= 0
|
jtulach@1334
|
2189 |
|| affix.indexOf(symbols.getDecimalSeparator()) >= 0
|
jtulach@1334
|
2190 |
|| affix.indexOf(symbols.getPercent()) >= 0
|
jtulach@1334
|
2191 |
|| affix.indexOf(symbols.getPerMill()) >= 0
|
jtulach@1334
|
2192 |
|| affix.indexOf(symbols.getDigit()) >= 0
|
jtulach@1334
|
2193 |
|| affix.indexOf(symbols.getPatternSeparator()) >= 0
|
jtulach@1334
|
2194 |
|| affix.indexOf(symbols.getMinusSign()) >= 0
|
jtulach@1334
|
2195 |
|| affix.indexOf(CURRENCY_SIGN) >= 0;
|
jtulach@1334
|
2196 |
}
|
jtulach@1334
|
2197 |
else {
|
jtulach@1334
|
2198 |
needQuote = affix.indexOf(PATTERN_ZERO_DIGIT) >= 0
|
jtulach@1334
|
2199 |
|| affix.indexOf(PATTERN_GROUPING_SEPARATOR) >= 0
|
jtulach@1334
|
2200 |
|| affix.indexOf(PATTERN_DECIMAL_SEPARATOR) >= 0
|
jtulach@1334
|
2201 |
|| affix.indexOf(PATTERN_PERCENT) >= 0
|
jtulach@1334
|
2202 |
|| affix.indexOf(PATTERN_PER_MILLE) >= 0
|
jtulach@1334
|
2203 |
|| affix.indexOf(PATTERN_DIGIT) >= 0
|
jtulach@1334
|
2204 |
|| affix.indexOf(PATTERN_SEPARATOR) >= 0
|
jtulach@1334
|
2205 |
|| affix.indexOf(PATTERN_MINUS) >= 0
|
jtulach@1334
|
2206 |
|| affix.indexOf(CURRENCY_SIGN) >= 0;
|
jtulach@1334
|
2207 |
}
|
jtulach@1334
|
2208 |
if (needQuote) buffer.append('\'');
|
jtulach@1334
|
2209 |
if (affix.indexOf('\'') < 0) buffer.append(affix);
|
jtulach@1334
|
2210 |
else {
|
jtulach@1334
|
2211 |
for (int j=0; j<affix.length(); ++j) {
|
jtulach@1334
|
2212 |
char c = affix.charAt(j);
|
jtulach@1334
|
2213 |
buffer.append(c);
|
jtulach@1334
|
2214 |
if (c == '\'') buffer.append(c);
|
jtulach@1334
|
2215 |
}
|
jtulach@1334
|
2216 |
}
|
jtulach@1334
|
2217 |
if (needQuote) buffer.append('\'');
|
jtulach@1334
|
2218 |
}
|
jtulach@1334
|
2219 |
|
jtulach@1334
|
2220 |
/**
|
jtulach@1334
|
2221 |
* Does the real work of generating a pattern. */
|
jtulach@1334
|
2222 |
private String toPattern(boolean localized) {
|
jtulach@1334
|
2223 |
StringBuffer result = new StringBuffer();
|
jtulach@1334
|
2224 |
for (int j = 1; j >= 0; --j) {
|
jtulach@1334
|
2225 |
if (j == 1)
|
jtulach@1334
|
2226 |
appendAffix(result, posPrefixPattern, positivePrefix, localized);
|
jtulach@1334
|
2227 |
else appendAffix(result, negPrefixPattern, negativePrefix, localized);
|
jtulach@1334
|
2228 |
int i;
|
jtulach@1334
|
2229 |
int digitCount = useExponentialNotation
|
jtulach@1334
|
2230 |
? getMaximumIntegerDigits()
|
jtulach@1334
|
2231 |
: Math.max(groupingSize, getMinimumIntegerDigits())+1;
|
jtulach@1334
|
2232 |
for (i = digitCount; i > 0; --i) {
|
jtulach@1334
|
2233 |
if (i != digitCount && isGroupingUsed() && groupingSize != 0 &&
|
jtulach@1334
|
2234 |
i % groupingSize == 0) {
|
jtulach@1334
|
2235 |
result.append(localized ? symbols.getGroupingSeparator() :
|
jtulach@1334
|
2236 |
PATTERN_GROUPING_SEPARATOR);
|
jtulach@1334
|
2237 |
}
|
jtulach@1334
|
2238 |
result.append(i <= getMinimumIntegerDigits()
|
jtulach@1334
|
2239 |
? (localized ? symbols.getZeroDigit() : PATTERN_ZERO_DIGIT)
|
jtulach@1334
|
2240 |
: (localized ? symbols.getDigit() : PATTERN_DIGIT));
|
jtulach@1334
|
2241 |
}
|
jtulach@1334
|
2242 |
if (getMaximumFractionDigits() > 0 || decimalSeparatorAlwaysShown)
|
jtulach@1334
|
2243 |
result.append(localized ? symbols.getDecimalSeparator() :
|
jtulach@1334
|
2244 |
PATTERN_DECIMAL_SEPARATOR);
|
jtulach@1334
|
2245 |
for (i = 0; i < getMaximumFractionDigits(); ++i) {
|
jtulach@1334
|
2246 |
if (i < getMinimumFractionDigits()) {
|
jtulach@1334
|
2247 |
result.append(localized ? symbols.getZeroDigit() :
|
jtulach@1334
|
2248 |
PATTERN_ZERO_DIGIT);
|
jtulach@1334
|
2249 |
} else {
|
jtulach@1334
|
2250 |
result.append(localized ? symbols.getDigit() :
|
jtulach@1334
|
2251 |
PATTERN_DIGIT);
|
jtulach@1334
|
2252 |
}
|
jtulach@1334
|
2253 |
}
|
jtulach@1334
|
2254 |
if (useExponentialNotation)
|
jtulach@1334
|
2255 |
{
|
jtulach@1334
|
2256 |
result.append(localized ? symbols.getExponentSeparator() :
|
jtulach@1334
|
2257 |
PATTERN_EXPONENT);
|
jtulach@1334
|
2258 |
for (i=0; i<minExponentDigits; ++i)
|
jtulach@1334
|
2259 |
result.append(localized ? symbols.getZeroDigit() :
|
jtulach@1334
|
2260 |
PATTERN_ZERO_DIGIT);
|
jtulach@1334
|
2261 |
}
|
jtulach@1334
|
2262 |
if (j == 1) {
|
jtulach@1334
|
2263 |
appendAffix(result, posSuffixPattern, positiveSuffix, localized);
|
jtulach@1334
|
2264 |
if ((negSuffixPattern == posSuffixPattern && // n == p == null
|
jtulach@1334
|
2265 |
negativeSuffix.equals(positiveSuffix))
|
jtulach@1334
|
2266 |
|| (negSuffixPattern != null &&
|
jtulach@1334
|
2267 |
negSuffixPattern.equals(posSuffixPattern))) {
|
jtulach@1334
|
2268 |
if ((negPrefixPattern != null && posPrefixPattern != null &&
|
jtulach@1334
|
2269 |
negPrefixPattern.equals("'-" + posPrefixPattern)) ||
|
jtulach@1334
|
2270 |
(negPrefixPattern == posPrefixPattern && // n == p == null
|
jtulach@1334
|
2271 |
negativePrefix.equals(symbols.getMinusSign() + positivePrefix)))
|
jtulach@1334
|
2272 |
break;
|
jtulach@1334
|
2273 |
}
|
jtulach@1334
|
2274 |
result.append(localized ? symbols.getPatternSeparator() :
|
jtulach@1334
|
2275 |
PATTERN_SEPARATOR);
|
jtulach@1334
|
2276 |
} else appendAffix(result, negSuffixPattern, negativeSuffix, localized);
|
jtulach@1334
|
2277 |
}
|
jtulach@1334
|
2278 |
return result.toString();
|
jtulach@1334
|
2279 |
}
|
jtulach@1334
|
2280 |
|
jtulach@1334
|
2281 |
/**
|
jtulach@1334
|
2282 |
* Apply the given pattern to this Format object. A pattern is a
|
jtulach@1334
|
2283 |
* short-hand specification for the various formatting properties.
|
jtulach@1334
|
2284 |
* These properties can also be changed individually through the
|
jtulach@1334
|
2285 |
* various setter methods.
|
jtulach@1334
|
2286 |
* <p>
|
jtulach@1334
|
2287 |
* There is no limit to integer digits set
|
jtulach@1334
|
2288 |
* by this routine, since that is the typical end-user desire;
|
jtulach@1334
|
2289 |
* use setMaximumInteger if you want to set a real value.
|
jtulach@1334
|
2290 |
* For negative numbers, use a second pattern, separated by a semicolon
|
jtulach@1334
|
2291 |
* <P>Example <code>"#,#00.0#"</code> -> 1,234.56
|
jtulach@1334
|
2292 |
* <P>This means a minimum of 2 integer digits, 1 fraction digit, and
|
jtulach@1334
|
2293 |
* a maximum of 2 fraction digits.
|
jtulach@1334
|
2294 |
* <p>Example: <code>"#,#00.0#;(#,#00.0#)"</code> for negatives in
|
jtulach@1334
|
2295 |
* parentheses.
|
jtulach@1334
|
2296 |
* <p>In negative patterns, the minimum and maximum counts are ignored;
|
jtulach@1334
|
2297 |
* these are presumed to be set in the positive pattern.
|
jtulach@1334
|
2298 |
*
|
jtulach@1334
|
2299 |
* @exception NullPointerException if <code>pattern</code> is null
|
jtulach@1334
|
2300 |
* @exception IllegalArgumentException if the given pattern is invalid.
|
jtulach@1334
|
2301 |
*/
|
jtulach@1334
|
2302 |
public void applyPattern(String pattern) {
|
jtulach@1334
|
2303 |
applyPattern(pattern, false);
|
jtulach@1334
|
2304 |
}
|
jtulach@1334
|
2305 |
|
jtulach@1334
|
2306 |
/**
|
jtulach@1334
|
2307 |
* Apply the given pattern to this Format object. The pattern
|
jtulach@1334
|
2308 |
* is assumed to be in a localized notation. A pattern is a
|
jtulach@1334
|
2309 |
* short-hand specification for the various formatting properties.
|
jtulach@1334
|
2310 |
* These properties can also be changed individually through the
|
jtulach@1334
|
2311 |
* various setter methods.
|
jtulach@1334
|
2312 |
* <p>
|
jtulach@1334
|
2313 |
* There is no limit to integer digits set
|
jtulach@1334
|
2314 |
* by this routine, since that is the typical end-user desire;
|
jtulach@1334
|
2315 |
* use setMaximumInteger if you want to set a real value.
|
jtulach@1334
|
2316 |
* For negative numbers, use a second pattern, separated by a semicolon
|
jtulach@1334
|
2317 |
* <P>Example <code>"#,#00.0#"</code> -> 1,234.56
|
jtulach@1334
|
2318 |
* <P>This means a minimum of 2 integer digits, 1 fraction digit, and
|
jtulach@1334
|
2319 |
* a maximum of 2 fraction digits.
|
jtulach@1334
|
2320 |
* <p>Example: <code>"#,#00.0#;(#,#00.0#)"</code> for negatives in
|
jtulach@1334
|
2321 |
* parentheses.
|
jtulach@1334
|
2322 |
* <p>In negative patterns, the minimum and maximum counts are ignored;
|
jtulach@1334
|
2323 |
* these are presumed to be set in the positive pattern.
|
jtulach@1334
|
2324 |
*
|
jtulach@1334
|
2325 |
* @exception NullPointerException if <code>pattern</code> is null
|
jtulach@1334
|
2326 |
* @exception IllegalArgumentException if the given pattern is invalid.
|
jtulach@1334
|
2327 |
*/
|
jtulach@1334
|
2328 |
public void applyLocalizedPattern(String pattern) {
|
jtulach@1334
|
2329 |
applyPattern(pattern, true);
|
jtulach@1334
|
2330 |
}
|
jtulach@1334
|
2331 |
|
jtulach@1334
|
2332 |
/**
|
jtulach@1334
|
2333 |
* Does the real work of applying a pattern.
|
jtulach@1334
|
2334 |
*/
|
jtulach@1334
|
2335 |
private void applyPattern(String pattern, boolean localized) {
|
jtulach@1334
|
2336 |
char zeroDigit = PATTERN_ZERO_DIGIT;
|
jtulach@1334
|
2337 |
char groupingSeparator = PATTERN_GROUPING_SEPARATOR;
|
jtulach@1334
|
2338 |
char decimalSeparator = PATTERN_DECIMAL_SEPARATOR;
|
jtulach@1334
|
2339 |
char percent = PATTERN_PERCENT;
|
jtulach@1334
|
2340 |
char perMill = PATTERN_PER_MILLE;
|
jtulach@1334
|
2341 |
char digit = PATTERN_DIGIT;
|
jtulach@1334
|
2342 |
char separator = PATTERN_SEPARATOR;
|
jtulach@1334
|
2343 |
String exponent = PATTERN_EXPONENT;
|
jtulach@1334
|
2344 |
char minus = PATTERN_MINUS;
|
jtulach@1334
|
2345 |
if (localized) {
|
jtulach@1334
|
2346 |
zeroDigit = symbols.getZeroDigit();
|
jtulach@1334
|
2347 |
groupingSeparator = symbols.getGroupingSeparator();
|
jtulach@1334
|
2348 |
decimalSeparator = symbols.getDecimalSeparator();
|
jtulach@1334
|
2349 |
percent = symbols.getPercent();
|
jtulach@1334
|
2350 |
perMill = symbols.getPerMill();
|
jtulach@1334
|
2351 |
digit = symbols.getDigit();
|
jtulach@1334
|
2352 |
separator = symbols.getPatternSeparator();
|
jtulach@1334
|
2353 |
exponent = symbols.getExponentSeparator();
|
jtulach@1334
|
2354 |
minus = symbols.getMinusSign();
|
jtulach@1334
|
2355 |
}
|
jtulach@1334
|
2356 |
boolean gotNegative = false;
|
jtulach@1334
|
2357 |
decimalSeparatorAlwaysShown = false;
|
jtulach@1334
|
2358 |
isCurrencyFormat = false;
|
jtulach@1334
|
2359 |
useExponentialNotation = false;
|
jtulach@1334
|
2360 |
|
jtulach@1334
|
2361 |
// Two variables are used to record the subrange of the pattern
|
jtulach@1334
|
2362 |
// occupied by phase 1. This is used during the processing of the
|
jtulach@1334
|
2363 |
// second pattern (the one representing negative numbers) to ensure
|
jtulach@1334
|
2364 |
// that no deviation exists in phase 1 between the two patterns.
|
jtulach@1334
|
2365 |
int phaseOneStart = 0;
|
jtulach@1334
|
2366 |
int phaseOneLength = 0;
|
jtulach@1334
|
2367 |
|
jtulach@1334
|
2368 |
int start = 0;
|
jtulach@1334
|
2369 |
for (int j = 1; j >= 0 && start < pattern.length(); --j) {
|
jtulach@1334
|
2370 |
boolean inQuote = false;
|
jtulach@1334
|
2371 |
StringBuffer prefix = new StringBuffer();
|
jtulach@1334
|
2372 |
StringBuffer suffix = new StringBuffer();
|
jtulach@1334
|
2373 |
int decimalPos = -1;
|
jtulach@1334
|
2374 |
int multiplier = 1;
|
jtulach@1334
|
2375 |
int digitLeftCount = 0, zeroDigitCount = 0, digitRightCount = 0;
|
jtulach@1334
|
2376 |
byte groupingCount = -1;
|
jtulach@1334
|
2377 |
|
jtulach@1334
|
2378 |
// The phase ranges from 0 to 2. Phase 0 is the prefix. Phase 1 is
|
jtulach@1334
|
2379 |
// the section of the pattern with digits, decimal separator,
|
jtulach@1334
|
2380 |
// grouping characters. Phase 2 is the suffix. In phases 0 and 2,
|
jtulach@1334
|
2381 |
// percent, per mille, and currency symbols are recognized and
|
jtulach@1334
|
2382 |
// translated. The separation of the characters into phases is
|
jtulach@1334
|
2383 |
// strictly enforced; if phase 1 characters are to appear in the
|
jtulach@1334
|
2384 |
// suffix, for example, they must be quoted.
|
jtulach@1334
|
2385 |
int phase = 0;
|
jtulach@1334
|
2386 |
|
jtulach@1334
|
2387 |
// The affix is either the prefix or the suffix.
|
jtulach@1334
|
2388 |
StringBuffer affix = prefix;
|
jtulach@1334
|
2389 |
|
jtulach@1334
|
2390 |
for (int pos = start; pos < pattern.length(); ++pos) {
|
jtulach@1334
|
2391 |
char ch = pattern.charAt(pos);
|
jtulach@1334
|
2392 |
switch (phase) {
|
jtulach@1334
|
2393 |
case 0:
|
jtulach@1334
|
2394 |
case 2:
|
jtulach@1334
|
2395 |
// Process the prefix / suffix characters
|
jtulach@1334
|
2396 |
if (inQuote) {
|
jtulach@1334
|
2397 |
// A quote within quotes indicates either the closing
|
jtulach@1334
|
2398 |
// quote or two quotes, which is a quote literal. That
|
jtulach@1334
|
2399 |
// is, we have the second quote in 'do' or 'don''t'.
|
jtulach@1334
|
2400 |
if (ch == QUOTE) {
|
jtulach@1334
|
2401 |
if ((pos+1) < pattern.length() &&
|
jtulach@1334
|
2402 |
pattern.charAt(pos+1) == QUOTE) {
|
jtulach@1334
|
2403 |
++pos;
|
jtulach@1334
|
2404 |
affix.append("''"); // 'don''t'
|
jtulach@1334
|
2405 |
} else {
|
jtulach@1334
|
2406 |
inQuote = false; // 'do'
|
jtulach@1334
|
2407 |
}
|
jtulach@1334
|
2408 |
continue;
|
jtulach@1334
|
2409 |
}
|
jtulach@1334
|
2410 |
} else {
|
jtulach@1334
|
2411 |
// Process unquoted characters seen in prefix or suffix
|
jtulach@1334
|
2412 |
// phase.
|
jtulach@1334
|
2413 |
if (ch == digit ||
|
jtulach@1334
|
2414 |
ch == zeroDigit ||
|
jtulach@1334
|
2415 |
ch == groupingSeparator ||
|
jtulach@1334
|
2416 |
ch == decimalSeparator) {
|
jtulach@1334
|
2417 |
phase = 1;
|
jtulach@1334
|
2418 |
if (j == 1) {
|
jtulach@1334
|
2419 |
phaseOneStart = pos;
|
jtulach@1334
|
2420 |
}
|
jtulach@1334
|
2421 |
--pos; // Reprocess this character
|
jtulach@1334
|
2422 |
continue;
|
jtulach@1334
|
2423 |
} else if (ch == CURRENCY_SIGN) {
|
jtulach@1334
|
2424 |
// Use lookahead to determine if the currency sign
|
jtulach@1334
|
2425 |
// is doubled or not.
|
jtulach@1334
|
2426 |
boolean doubled = (pos + 1) < pattern.length() &&
|
jtulach@1334
|
2427 |
pattern.charAt(pos + 1) == CURRENCY_SIGN;
|
jtulach@1334
|
2428 |
if (doubled) { // Skip over the doubled character
|
jtulach@1334
|
2429 |
++pos;
|
jtulach@1334
|
2430 |
}
|
jtulach@1334
|
2431 |
isCurrencyFormat = true;
|
jtulach@1334
|
2432 |
affix.append(doubled ? "'\u00A4\u00A4" : "'\u00A4");
|
jtulach@1334
|
2433 |
continue;
|
jtulach@1334
|
2434 |
} else if (ch == QUOTE) {
|
jtulach@1334
|
2435 |
// A quote outside quotes indicates either the
|
jtulach@1334
|
2436 |
// opening quote or two quotes, which is a quote
|
jtulach@1334
|
2437 |
// literal. That is, we have the first quote in 'do'
|
jtulach@1334
|
2438 |
// or o''clock.
|
jtulach@1334
|
2439 |
if (ch == QUOTE) {
|
jtulach@1334
|
2440 |
if ((pos+1) < pattern.length() &&
|
jtulach@1334
|
2441 |
pattern.charAt(pos+1) == QUOTE) {
|
jtulach@1334
|
2442 |
++pos;
|
jtulach@1334
|
2443 |
affix.append("''"); // o''clock
|
jtulach@1334
|
2444 |
} else {
|
jtulach@1334
|
2445 |
inQuote = true; // 'do'
|
jtulach@1334
|
2446 |
}
|
jtulach@1334
|
2447 |
continue;
|
jtulach@1334
|
2448 |
}
|
jtulach@1334
|
2449 |
} else if (ch == separator) {
|
jtulach@1334
|
2450 |
// Don't allow separators before we see digit
|
jtulach@1334
|
2451 |
// characters of phase 1, and don't allow separators
|
jtulach@1334
|
2452 |
// in the second pattern (j == 0).
|
jtulach@1334
|
2453 |
if (phase == 0 || j == 0) {
|
jtulach@1334
|
2454 |
throw new IllegalArgumentException("Unquoted special character '" +
|
jtulach@1334
|
2455 |
ch + "' in pattern \"" + pattern + '"');
|
jtulach@1334
|
2456 |
}
|
jtulach@1334
|
2457 |
start = pos + 1;
|
jtulach@1334
|
2458 |
pos = pattern.length();
|
jtulach@1334
|
2459 |
continue;
|
jtulach@1334
|
2460 |
}
|
jtulach@1334
|
2461 |
|
jtulach@1334
|
2462 |
// Next handle characters which are appended directly.
|
jtulach@1334
|
2463 |
else if (ch == percent) {
|
jtulach@1334
|
2464 |
if (multiplier != 1) {
|
jtulach@1334
|
2465 |
throw new IllegalArgumentException("Too many percent/per mille characters in pattern \"" +
|
jtulach@1334
|
2466 |
pattern + '"');
|
jtulach@1334
|
2467 |
}
|
jtulach@1334
|
2468 |
multiplier = 100;
|
jtulach@1334
|
2469 |
affix.append("'%");
|
jtulach@1334
|
2470 |
continue;
|
jtulach@1334
|
2471 |
} else if (ch == perMill) {
|
jtulach@1334
|
2472 |
if (multiplier != 1) {
|
jtulach@1334
|
2473 |
throw new IllegalArgumentException("Too many percent/per mille characters in pattern \"" +
|
jtulach@1334
|
2474 |
pattern + '"');
|
jtulach@1334
|
2475 |
}
|
jtulach@1334
|
2476 |
multiplier = 1000;
|
jtulach@1334
|
2477 |
affix.append("'\u2030");
|
jtulach@1334
|
2478 |
continue;
|
jtulach@1334
|
2479 |
} else if (ch == minus) {
|
jtulach@1334
|
2480 |
affix.append("'-");
|
jtulach@1334
|
2481 |
continue;
|
jtulach@1334
|
2482 |
}
|
jtulach@1334
|
2483 |
}
|
jtulach@1334
|
2484 |
// Note that if we are within quotes, or if this is an
|
jtulach@1334
|
2485 |
// unquoted, non-special character, then we usually fall
|
jtulach@1334
|
2486 |
// through to here.
|
jtulach@1334
|
2487 |
affix.append(ch);
|
jtulach@1334
|
2488 |
break;
|
jtulach@1334
|
2489 |
|
jtulach@1334
|
2490 |
case 1:
|
jtulach@1334
|
2491 |
// Phase one must be identical in the two sub-patterns. We
|
jtulach@1334
|
2492 |
// enforce this by doing a direct comparison. While
|
jtulach@1334
|
2493 |
// processing the first sub-pattern, we just record its
|
jtulach@1334
|
2494 |
// length. While processing the second, we compare
|
jtulach@1334
|
2495 |
// characters.
|
jtulach@1334
|
2496 |
if (j == 1) {
|
jtulach@1334
|
2497 |
++phaseOneLength;
|
jtulach@1334
|
2498 |
} else {
|
jtulach@1334
|
2499 |
if (--phaseOneLength == 0) {
|
jtulach@1334
|
2500 |
phase = 2;
|
jtulach@1334
|
2501 |
affix = suffix;
|
jtulach@1334
|
2502 |
}
|
jtulach@1334
|
2503 |
continue;
|
jtulach@1334
|
2504 |
}
|
jtulach@1334
|
2505 |
|
jtulach@1334
|
2506 |
// Process the digits, decimal, and grouping characters. We
|
jtulach@1334
|
2507 |
// record five pieces of information. We expect the digits
|
jtulach@1334
|
2508 |
// to occur in the pattern ####0000.####, and we record the
|
jtulach@1334
|
2509 |
// number of left digits, zero (central) digits, and right
|
jtulach@1334
|
2510 |
// digits. The position of the last grouping character is
|
jtulach@1334
|
2511 |
// recorded (should be somewhere within the first two blocks
|
jtulach@1334
|
2512 |
// of characters), as is the position of the decimal point,
|
jtulach@1334
|
2513 |
// if any (should be in the zero digits). If there is no
|
jtulach@1334
|
2514 |
// decimal point, then there should be no right digits.
|
jtulach@1334
|
2515 |
if (ch == digit) {
|
jtulach@1334
|
2516 |
if (zeroDigitCount > 0) {
|
jtulach@1334
|
2517 |
++digitRightCount;
|
jtulach@1334
|
2518 |
} else {
|
jtulach@1334
|
2519 |
++digitLeftCount;
|
jtulach@1334
|
2520 |
}
|
jtulach@1334
|
2521 |
if (groupingCount >= 0 && decimalPos < 0) {
|
jtulach@1334
|
2522 |
++groupingCount;
|
jtulach@1334
|
2523 |
}
|
jtulach@1334
|
2524 |
} else if (ch == zeroDigit) {
|
jtulach@1334
|
2525 |
if (digitRightCount > 0) {
|
jtulach@1334
|
2526 |
throw new IllegalArgumentException("Unexpected '0' in pattern \"" +
|
jtulach@1334
|
2527 |
pattern + '"');
|
jtulach@1334
|
2528 |
}
|
jtulach@1334
|
2529 |
++zeroDigitCount;
|
jtulach@1334
|
2530 |
if (groupingCount >= 0 && decimalPos < 0) {
|
jtulach@1334
|
2531 |
++groupingCount;
|
jtulach@1334
|
2532 |
}
|
jtulach@1334
|
2533 |
} else if (ch == groupingSeparator) {
|
jtulach@1334
|
2534 |
groupingCount = 0;
|
jtulach@1334
|
2535 |
} else if (ch == decimalSeparator) {
|
jtulach@1334
|
2536 |
if (decimalPos >= 0) {
|
jtulach@1334
|
2537 |
throw new IllegalArgumentException("Multiple decimal separators in pattern \"" +
|
jtulach@1334
|
2538 |
pattern + '"');
|
jtulach@1334
|
2539 |
}
|
jtulach@1334
|
2540 |
decimalPos = digitLeftCount + zeroDigitCount + digitRightCount;
|
jtulach@1334
|
2541 |
} else if (pattern.regionMatches(pos, exponent, 0, exponent.length())){
|
jtulach@1334
|
2542 |
if (useExponentialNotation) {
|
jtulach@1334
|
2543 |
throw new IllegalArgumentException("Multiple exponential " +
|
jtulach@1334
|
2544 |
"symbols in pattern \"" + pattern + '"');
|
jtulach@1334
|
2545 |
}
|
jtulach@1334
|
2546 |
useExponentialNotation = true;
|
jtulach@1334
|
2547 |
minExponentDigits = 0;
|
jtulach@1334
|
2548 |
|
jtulach@1334
|
2549 |
// Use lookahead to parse out the exponential part
|
jtulach@1334
|
2550 |
// of the pattern, then jump into phase 2.
|
jtulach@1334
|
2551 |
pos = pos+exponent.length();
|
jtulach@1334
|
2552 |
while (pos < pattern.length() &&
|
jtulach@1334
|
2553 |
pattern.charAt(pos) == zeroDigit) {
|
jtulach@1334
|
2554 |
++minExponentDigits;
|
jtulach@1334
|
2555 |
++phaseOneLength;
|
jtulach@1334
|
2556 |
++pos;
|
jtulach@1334
|
2557 |
}
|
jtulach@1334
|
2558 |
|
jtulach@1334
|
2559 |
if ((digitLeftCount + zeroDigitCount) < 1 ||
|
jtulach@1334
|
2560 |
minExponentDigits < 1) {
|
jtulach@1334
|
2561 |
throw new IllegalArgumentException("Malformed exponential " +
|
jtulach@1334
|
2562 |
"pattern \"" + pattern + '"');
|
jtulach@1334
|
2563 |
}
|
jtulach@1334
|
2564 |
|
jtulach@1334
|
2565 |
// Transition to phase 2
|
jtulach@1334
|
2566 |
phase = 2;
|
jtulach@1334
|
2567 |
affix = suffix;
|
jtulach@1334
|
2568 |
--pos;
|
jtulach@1334
|
2569 |
continue;
|
jtulach@1334
|
2570 |
} else {
|
jtulach@1334
|
2571 |
phase = 2;
|
jtulach@1334
|
2572 |
affix = suffix;
|
jtulach@1334
|
2573 |
--pos;
|
jtulach@1334
|
2574 |
--phaseOneLength;
|
jtulach@1334
|
2575 |
continue;
|
jtulach@1334
|
2576 |
}
|
jtulach@1334
|
2577 |
break;
|
jtulach@1334
|
2578 |
}
|
jtulach@1334
|
2579 |
}
|
jtulach@1334
|
2580 |
|
jtulach@1334
|
2581 |
// Handle patterns with no '0' pattern character. These patterns
|
jtulach@1334
|
2582 |
// are legal, but must be interpreted. "##.###" -> "#0.###".
|
jtulach@1334
|
2583 |
// ".###" -> ".0##".
|
jtulach@1334
|
2584 |
/* We allow patterns of the form "####" to produce a zeroDigitCount
|
jtulach@1334
|
2585 |
* of zero (got that?); although this seems like it might make it
|
jtulach@1334
|
2586 |
* possible for format() to produce empty strings, format() checks
|
jtulach@1334
|
2587 |
* for this condition and outputs a zero digit in this situation.
|
jtulach@1334
|
2588 |
* Having a zeroDigitCount of zero yields a minimum integer digits
|
jtulach@1334
|
2589 |
* of zero, which allows proper round-trip patterns. That is, we
|
jtulach@1334
|
2590 |
* don't want "#" to become "#0" when toPattern() is called (even
|
jtulach@1334
|
2591 |
* though that's what it really is, semantically).
|
jtulach@1334
|
2592 |
*/
|
jtulach@1334
|
2593 |
if (zeroDigitCount == 0 && digitLeftCount > 0 && decimalPos >= 0) {
|
jtulach@1334
|
2594 |
// Handle "###.###" and "###." and ".###"
|
jtulach@1334
|
2595 |
int n = decimalPos;
|
jtulach@1334
|
2596 |
if (n == 0) { // Handle ".###"
|
jtulach@1334
|
2597 |
++n;
|
jtulach@1334
|
2598 |
}
|
jtulach@1334
|
2599 |
digitRightCount = digitLeftCount - n;
|
jtulach@1334
|
2600 |
digitLeftCount = n - 1;
|
jtulach@1334
|
2601 |
zeroDigitCount = 1;
|
jtulach@1334
|
2602 |
}
|
jtulach@1334
|
2603 |
|
jtulach@1334
|
2604 |
// Do syntax checking on the digits.
|
jtulach@1334
|
2605 |
if ((decimalPos < 0 && digitRightCount > 0) ||
|
jtulach@1334
|
2606 |
(decimalPos >= 0 && (decimalPos < digitLeftCount ||
|
jtulach@1334
|
2607 |
decimalPos > (digitLeftCount + zeroDigitCount))) ||
|
jtulach@1334
|
2608 |
groupingCount == 0 || inQuote) {
|
jtulach@1334
|
2609 |
throw new IllegalArgumentException("Malformed pattern \"" +
|
jtulach@1334
|
2610 |
pattern + '"');
|
jtulach@1334
|
2611 |
}
|
jtulach@1334
|
2612 |
|
jtulach@1334
|
2613 |
if (j == 1) {
|
jtulach@1334
|
2614 |
posPrefixPattern = prefix.toString();
|
jtulach@1334
|
2615 |
posSuffixPattern = suffix.toString();
|
jtulach@1334
|
2616 |
negPrefixPattern = posPrefixPattern; // assume these for now
|
jtulach@1334
|
2617 |
negSuffixPattern = posSuffixPattern;
|
jtulach@1334
|
2618 |
int digitTotalCount = digitLeftCount + zeroDigitCount + digitRightCount;
|
jtulach@1334
|
2619 |
/* The effectiveDecimalPos is the position the decimal is at or
|
jtulach@1334
|
2620 |
* would be at if there is no decimal. Note that if decimalPos<0,
|
jtulach@1334
|
2621 |
* then digitTotalCount == digitLeftCount + zeroDigitCount.
|
jtulach@1334
|
2622 |
*/
|
jtulach@1334
|
2623 |
int effectiveDecimalPos = decimalPos >= 0 ?
|
jtulach@1334
|
2624 |
decimalPos : digitTotalCount;
|
jtulach@1334
|
2625 |
setMinimumIntegerDigits(effectiveDecimalPos - digitLeftCount);
|
jtulach@1334
|
2626 |
setMaximumIntegerDigits(useExponentialNotation ?
|
jtulach@1334
|
2627 |
digitLeftCount + getMinimumIntegerDigits() :
|
jtulach@1334
|
2628 |
MAXIMUM_INTEGER_DIGITS);
|
jtulach@1334
|
2629 |
setMaximumFractionDigits(decimalPos >= 0 ?
|
jtulach@1334
|
2630 |
(digitTotalCount - decimalPos) : 0);
|
jtulach@1334
|
2631 |
setMinimumFractionDigits(decimalPos >= 0 ?
|
jtulach@1334
|
2632 |
(digitLeftCount + zeroDigitCount - decimalPos) : 0);
|
jtulach@1334
|
2633 |
setGroupingUsed(groupingCount > 0);
|
jtulach@1334
|
2634 |
this.groupingSize = (groupingCount > 0) ? groupingCount : 0;
|
jtulach@1334
|
2635 |
this.multiplier = multiplier;
|
jtulach@1334
|
2636 |
setDecimalSeparatorAlwaysShown(decimalPos == 0 ||
|
jtulach@1334
|
2637 |
decimalPos == digitTotalCount);
|
jtulach@1334
|
2638 |
} else {
|
jtulach@1334
|
2639 |
negPrefixPattern = prefix.toString();
|
jtulach@1334
|
2640 |
negSuffixPattern = suffix.toString();
|
jtulach@1334
|
2641 |
gotNegative = true;
|
jtulach@1334
|
2642 |
}
|
jtulach@1334
|
2643 |
}
|
jtulach@1334
|
2644 |
|
jtulach@1334
|
2645 |
if (pattern.length() == 0) {
|
jtulach@1334
|
2646 |
posPrefixPattern = posSuffixPattern = "";
|
jtulach@1334
|
2647 |
setMinimumIntegerDigits(0);
|
jtulach@1334
|
2648 |
setMaximumIntegerDigits(MAXIMUM_INTEGER_DIGITS);
|
jtulach@1334
|
2649 |
setMinimumFractionDigits(0);
|
jtulach@1334
|
2650 |
setMaximumFractionDigits(MAXIMUM_FRACTION_DIGITS);
|
jtulach@1334
|
2651 |
}
|
jtulach@1334
|
2652 |
|
jtulach@1334
|
2653 |
// If there was no negative pattern, or if the negative pattern is
|
jtulach@1334
|
2654 |
// identical to the positive pattern, then prepend the minus sign to
|
jtulach@1334
|
2655 |
// the positive pattern to form the negative pattern.
|
jtulach@1334
|
2656 |
if (!gotNegative ||
|
jtulach@1334
|
2657 |
(negPrefixPattern.equals(posPrefixPattern)
|
jtulach@1334
|
2658 |
&& negSuffixPattern.equals(posSuffixPattern))) {
|
jtulach@1334
|
2659 |
negSuffixPattern = posSuffixPattern;
|
jtulach@1334
|
2660 |
negPrefixPattern = "'-" + posPrefixPattern;
|
jtulach@1334
|
2661 |
}
|
jtulach@1334
|
2662 |
|
jtulach@1334
|
2663 |
expandAffixes();
|
jtulach@1334
|
2664 |
}
|
jtulach@1334
|
2665 |
|
jtulach@1334
|
2666 |
/**
|
jtulach@1334
|
2667 |
* Sets the maximum number of digits allowed in the integer portion of a
|
jtulach@1334
|
2668 |
* number.
|
jtulach@1334
|
2669 |
* For formatting numbers other than <code>BigInteger</code> and
|
jtulach@1334
|
2670 |
* <code>BigDecimal</code> objects, the lower of <code>newValue</code> and
|
jtulach@1334
|
2671 |
* 309 is used. Negative input values are replaced with 0.
|
jtulach@1334
|
2672 |
* @see NumberFormat#setMaximumIntegerDigits
|
jtulach@1334
|
2673 |
*/
|
jtulach@1334
|
2674 |
public void setMaximumIntegerDigits(int newValue) {
|
jtulach@1334
|
2675 |
maximumIntegerDigits = Math.min(Math.max(0, newValue), MAXIMUM_INTEGER_DIGITS);
|
jtulach@1334
|
2676 |
super.setMaximumIntegerDigits((maximumIntegerDigits > DOUBLE_INTEGER_DIGITS) ?
|
jtulach@1334
|
2677 |
DOUBLE_INTEGER_DIGITS : maximumIntegerDigits);
|
jtulach@1334
|
2678 |
if (minimumIntegerDigits > maximumIntegerDigits) {
|
jtulach@1334
|
2679 |
minimumIntegerDigits = maximumIntegerDigits;
|
jtulach@1334
|
2680 |
super.setMinimumIntegerDigits((minimumIntegerDigits > DOUBLE_INTEGER_DIGITS) ?
|
jtulach@1334
|
2681 |
DOUBLE_INTEGER_DIGITS : minimumIntegerDigits);
|
jtulach@1334
|
2682 |
}
|
jtulach@1334
|
2683 |
}
|
jtulach@1334
|
2684 |
|
jtulach@1334
|
2685 |
/**
|
jtulach@1334
|
2686 |
* Sets the minimum number of digits allowed in the integer portion of a
|
jtulach@1334
|
2687 |
* number.
|
jtulach@1334
|
2688 |
* For formatting numbers other than <code>BigInteger</code> and
|
jtulach@1334
|
2689 |
* <code>BigDecimal</code> objects, the lower of <code>newValue</code> and
|
jtulach@1334
|
2690 |
* 309 is used. Negative input values are replaced with 0.
|
jtulach@1334
|
2691 |
* @see NumberFormat#setMinimumIntegerDigits
|
jtulach@1334
|
2692 |
*/
|
jtulach@1334
|
2693 |
public void setMinimumIntegerDigits(int newValue) {
|
jtulach@1334
|
2694 |
minimumIntegerDigits = Math.min(Math.max(0, newValue), MAXIMUM_INTEGER_DIGITS);
|
jtulach@1334
|
2695 |
super.setMinimumIntegerDigits((minimumIntegerDigits > DOUBLE_INTEGER_DIGITS) ?
|
jtulach@1334
|
2696 |
DOUBLE_INTEGER_DIGITS : minimumIntegerDigits);
|
jtulach@1334
|
2697 |
if (minimumIntegerDigits > maximumIntegerDigits) {
|
jtulach@1334
|
2698 |
maximumIntegerDigits = minimumIntegerDigits;
|
jtulach@1334
|
2699 |
super.setMaximumIntegerDigits((maximumIntegerDigits > DOUBLE_INTEGER_DIGITS) ?
|
jtulach@1334
|
2700 |
DOUBLE_INTEGER_DIGITS : maximumIntegerDigits);
|
jtulach@1334
|
2701 |
}
|
jtulach@1334
|
2702 |
}
|
jtulach@1334
|
2703 |
|
jtulach@1334
|
2704 |
/**
|
jtulach@1334
|
2705 |
* Sets the maximum number of digits allowed in the fraction portion of a
|
jtulach@1334
|
2706 |
* number.
|
jtulach@1334
|
2707 |
* For formatting numbers other than <code>BigInteger</code> and
|
jtulach@1334
|
2708 |
* <code>BigDecimal</code> objects, the lower of <code>newValue</code> and
|
jtulach@1334
|
2709 |
* 340 is used. Negative input values are replaced with 0.
|
jtulach@1334
|
2710 |
* @see NumberFormat#setMaximumFractionDigits
|
jtulach@1334
|
2711 |
*/
|
jtulach@1334
|
2712 |
public void setMaximumFractionDigits(int newValue) {
|
jtulach@1334
|
2713 |
maximumFractionDigits = Math.min(Math.max(0, newValue), MAXIMUM_FRACTION_DIGITS);
|
jtulach@1334
|
2714 |
super.setMaximumFractionDigits((maximumFractionDigits > DOUBLE_FRACTION_DIGITS) ?
|
jtulach@1334
|
2715 |
DOUBLE_FRACTION_DIGITS : maximumFractionDigits);
|
jtulach@1334
|
2716 |
if (minimumFractionDigits > maximumFractionDigits) {
|
jtulach@1334
|
2717 |
minimumFractionDigits = maximumFractionDigits;
|
jtulach@1334
|
2718 |
super.setMinimumFractionDigits((minimumFractionDigits > DOUBLE_FRACTION_DIGITS) ?
|
jtulach@1334
|
2719 |
DOUBLE_FRACTION_DIGITS : minimumFractionDigits);
|
jtulach@1334
|
2720 |
}
|
jtulach@1334
|
2721 |
}
|
jtulach@1334
|
2722 |
|
jtulach@1334
|
2723 |
/**
|
jtulach@1334
|
2724 |
* Sets the minimum number of digits allowed in the fraction portion of a
|
jtulach@1334
|
2725 |
* number.
|
jtulach@1334
|
2726 |
* For formatting numbers other than <code>BigInteger</code> and
|
jtulach@1334
|
2727 |
* <code>BigDecimal</code> objects, the lower of <code>newValue</code> and
|
jtulach@1334
|
2728 |
* 340 is used. Negative input values are replaced with 0.
|
jtulach@1334
|
2729 |
* @see NumberFormat#setMinimumFractionDigits
|
jtulach@1334
|
2730 |
*/
|
jtulach@1334
|
2731 |
public void setMinimumFractionDigits(int newValue) {
|
jtulach@1334
|
2732 |
minimumFractionDigits = Math.min(Math.max(0, newValue), MAXIMUM_FRACTION_DIGITS);
|
jtulach@1334
|
2733 |
super.setMinimumFractionDigits((minimumFractionDigits > DOUBLE_FRACTION_DIGITS) ?
|
jtulach@1334
|
2734 |
DOUBLE_FRACTION_DIGITS : minimumFractionDigits);
|
jtulach@1334
|
2735 |
if (minimumFractionDigits > maximumFractionDigits) {
|
jtulach@1334
|
2736 |
maximumFractionDigits = minimumFractionDigits;
|
jtulach@1334
|
2737 |
super.setMaximumFractionDigits((maximumFractionDigits > DOUBLE_FRACTION_DIGITS) ?
|
jtulach@1334
|
2738 |
DOUBLE_FRACTION_DIGITS : maximumFractionDigits);
|
jtulach@1334
|
2739 |
}
|
jtulach@1334
|
2740 |
}
|
jtulach@1334
|
2741 |
|
jtulach@1334
|
2742 |
/**
|
jtulach@1334
|
2743 |
* Gets the maximum number of digits allowed in the integer portion of a
|
jtulach@1334
|
2744 |
* number.
|
jtulach@1334
|
2745 |
* For formatting numbers other than <code>BigInteger</code> and
|
jtulach@1334
|
2746 |
* <code>BigDecimal</code> objects, the lower of the return value and
|
jtulach@1334
|
2747 |
* 309 is used.
|
jtulach@1334
|
2748 |
* @see #setMaximumIntegerDigits
|
jtulach@1334
|
2749 |
*/
|
jtulach@1334
|
2750 |
public int getMaximumIntegerDigits() {
|
jtulach@1334
|
2751 |
return maximumIntegerDigits;
|
jtulach@1334
|
2752 |
}
|
jtulach@1334
|
2753 |
|
jtulach@1334
|
2754 |
/**
|
jtulach@1334
|
2755 |
* Gets the minimum number of digits allowed in the integer portion of a
|
jtulach@1334
|
2756 |
* number.
|
jtulach@1334
|
2757 |
* For formatting numbers other than <code>BigInteger</code> and
|
jtulach@1334
|
2758 |
* <code>BigDecimal</code> objects, the lower of the return value and
|
jtulach@1334
|
2759 |
* 309 is used.
|
jtulach@1334
|
2760 |
* @see #setMinimumIntegerDigits
|
jtulach@1334
|
2761 |
*/
|
jtulach@1334
|
2762 |
public int getMinimumIntegerDigits() {
|
jtulach@1334
|
2763 |
return minimumIntegerDigits;
|
jtulach@1334
|
2764 |
}
|
jtulach@1334
|
2765 |
|
jtulach@1334
|
2766 |
/**
|
jtulach@1334
|
2767 |
* Gets the maximum number of digits allowed in the fraction portion of a
|
jtulach@1334
|
2768 |
* number.
|
jtulach@1334
|
2769 |
* For formatting numbers other than <code>BigInteger</code> and
|
jtulach@1334
|
2770 |
* <code>BigDecimal</code> objects, the lower of the return value and
|
jtulach@1334
|
2771 |
* 340 is used.
|
jtulach@1334
|
2772 |
* @see #setMaximumFractionDigits
|
jtulach@1334
|
2773 |
*/
|
jtulach@1334
|
2774 |
public int getMaximumFractionDigits() {
|
jtulach@1334
|
2775 |
return maximumFractionDigits;
|
jtulach@1334
|
2776 |
}
|
jtulach@1334
|
2777 |
|
jtulach@1334
|
2778 |
/**
|
jtulach@1334
|
2779 |
* Gets the minimum number of digits allowed in the fraction portion of a
|
jtulach@1334
|
2780 |
* number.
|
jtulach@1334
|
2781 |
* For formatting numbers other than <code>BigInteger</code> and
|
jtulach@1334
|
2782 |
* <code>BigDecimal</code> objects, the lower of the return value and
|
jtulach@1334
|
2783 |
* 340 is used.
|
jtulach@1334
|
2784 |
* @see #setMinimumFractionDigits
|
jtulach@1334
|
2785 |
*/
|
jtulach@1334
|
2786 |
public int getMinimumFractionDigits() {
|
jtulach@1334
|
2787 |
return minimumFractionDigits;
|
jtulach@1334
|
2788 |
}
|
jtulach@1334
|
2789 |
|
jtulach@1334
|
2790 |
/**
|
jtulach@1334
|
2791 |
* Gets the currency used by this decimal format when formatting
|
jtulach@1334
|
2792 |
* currency values.
|
jtulach@1334
|
2793 |
* The currency is obtained by calling
|
jtulach@1334
|
2794 |
* {@link DecimalFormatSymbols#getCurrency DecimalFormatSymbols.getCurrency}
|
jtulach@1334
|
2795 |
* on this number format's symbols.
|
jtulach@1334
|
2796 |
*
|
jtulach@1334
|
2797 |
* @return the currency used by this decimal format, or <code>null</code>
|
jtulach@1334
|
2798 |
* @since 1.4
|
jtulach@1334
|
2799 |
*/
|
jtulach@1334
|
2800 |
public Currency getCurrency() {
|
jtulach@1334
|
2801 |
return symbols.getCurrency();
|
jtulach@1334
|
2802 |
}
|
jtulach@1334
|
2803 |
|
jtulach@1334
|
2804 |
/**
|
jtulach@1334
|
2805 |
* Sets the currency used by this number format when formatting
|
jtulach@1334
|
2806 |
* currency values. This does not update the minimum or maximum
|
jtulach@1334
|
2807 |
* number of fraction digits used by the number format.
|
jtulach@1334
|
2808 |
* The currency is set by calling
|
jtulach@1334
|
2809 |
* {@link DecimalFormatSymbols#setCurrency DecimalFormatSymbols.setCurrency}
|
jtulach@1334
|
2810 |
* on this number format's symbols.
|
jtulach@1334
|
2811 |
*
|
jtulach@1334
|
2812 |
* @param currency the new currency to be used by this decimal format
|
jtulach@1334
|
2813 |
* @exception NullPointerException if <code>currency</code> is null
|
jtulach@1334
|
2814 |
* @since 1.4
|
jtulach@1334
|
2815 |
*/
|
jtulach@1334
|
2816 |
public void setCurrency(Currency currency) {
|
jtulach@1334
|
2817 |
if (currency != symbols.getCurrency()) {
|
jtulach@1334
|
2818 |
symbols.setCurrency(currency);
|
jtulach@1334
|
2819 |
if (isCurrencyFormat) {
|
jtulach@1334
|
2820 |
expandAffixes();
|
jtulach@1334
|
2821 |
}
|
jtulach@1334
|
2822 |
}
|
jtulach@1334
|
2823 |
}
|
jtulach@1334
|
2824 |
|
jtulach@1334
|
2825 |
/**
|
jtulach@1334
|
2826 |
* Gets the {@link java.math.RoundingMode} used in this DecimalFormat.
|
jtulach@1334
|
2827 |
*
|
jtulach@1334
|
2828 |
* @return The <code>RoundingMode</code> used for this DecimalFormat.
|
jtulach@1334
|
2829 |
* @see #setRoundingMode(RoundingMode)
|
jtulach@1334
|
2830 |
* @since 1.6
|
jtulach@1334
|
2831 |
*/
|
jtulach@1334
|
2832 |
public RoundingMode getRoundingMode() {
|
jtulach@1334
|
2833 |
return roundingMode;
|
jtulach@1334
|
2834 |
}
|
jtulach@1334
|
2835 |
|
jtulach@1334
|
2836 |
/**
|
jtulach@1334
|
2837 |
* Sets the {@link java.math.RoundingMode} used in this DecimalFormat.
|
jtulach@1334
|
2838 |
*
|
jtulach@1334
|
2839 |
* @param roundingMode The <code>RoundingMode</code> to be used
|
jtulach@1334
|
2840 |
* @see #getRoundingMode()
|
jtulach@1334
|
2841 |
* @exception NullPointerException if <code>roundingMode</code> is null.
|
jtulach@1334
|
2842 |
* @since 1.6
|
jtulach@1334
|
2843 |
*/
|
jtulach@1334
|
2844 |
public void setRoundingMode(RoundingMode roundingMode) {
|
jtulach@1334
|
2845 |
if (roundingMode == null) {
|
jtulach@1334
|
2846 |
throw new NullPointerException();
|
jtulach@1334
|
2847 |
}
|
jtulach@1334
|
2848 |
|
jtulach@1334
|
2849 |
this.roundingMode = roundingMode;
|
jtulach@1334
|
2850 |
digitList.setRoundingMode(roundingMode);
|
jtulach@1334
|
2851 |
}
|
jtulach@1334
|
2852 |
|
jtulach@1334
|
2853 |
/**
|
jtulach@1334
|
2854 |
* Adjusts the minimum and maximum fraction digits to values that
|
jtulach@1334
|
2855 |
* are reasonable for the currency's default fraction digits.
|
jtulach@1334
|
2856 |
*/
|
jtulach@1334
|
2857 |
void adjustForCurrencyDefaultFractionDigits() {
|
jtulach@1334
|
2858 |
Currency currency = symbols.getCurrency();
|
jtulach@1334
|
2859 |
if (currency == null) {
|
jtulach@1334
|
2860 |
try {
|
jtulach@1334
|
2861 |
currency = Currency.getInstance(symbols.getInternationalCurrencySymbol());
|
jtulach@1334
|
2862 |
} catch (IllegalArgumentException e) {
|
jtulach@1334
|
2863 |
}
|
jtulach@1334
|
2864 |
}
|
jtulach@1334
|
2865 |
if (currency != null) {
|
jtulach@1334
|
2866 |
int digits = currency.getDefaultFractionDigits();
|
jtulach@1334
|
2867 |
if (digits != -1) {
|
jtulach@1334
|
2868 |
int oldMinDigits = getMinimumFractionDigits();
|
jtulach@1334
|
2869 |
// Common patterns are "#.##", "#.00", "#".
|
jtulach@1334
|
2870 |
// Try to adjust all of them in a reasonable way.
|
jtulach@1334
|
2871 |
if (oldMinDigits == getMaximumFractionDigits()) {
|
jtulach@1334
|
2872 |
setMinimumFractionDigits(digits);
|
jtulach@1334
|
2873 |
setMaximumFractionDigits(digits);
|
jtulach@1334
|
2874 |
} else {
|
jtulach@1334
|
2875 |
setMinimumFractionDigits(Math.min(digits, oldMinDigits));
|
jtulach@1334
|
2876 |
setMaximumFractionDigits(digits);
|
jtulach@1334
|
2877 |
}
|
jtulach@1334
|
2878 |
}
|
jtulach@1334
|
2879 |
}
|
jtulach@1334
|
2880 |
}
|
jtulach@1334
|
2881 |
|
jtulach@1334
|
2882 |
/**
|
jtulach@1334
|
2883 |
* Reads the default serializable fields from the stream and performs
|
jtulach@1334
|
2884 |
* validations and adjustments for older serialized versions. The
|
jtulach@1334
|
2885 |
* validations and adjustments are:
|
jtulach@1334
|
2886 |
* <ol>
|
jtulach@1334
|
2887 |
* <li>
|
jtulach@1334
|
2888 |
* Verify that the superclass's digit count fields correctly reflect
|
jtulach@1334
|
2889 |
* the limits imposed on formatting numbers other than
|
jtulach@1334
|
2890 |
* <code>BigInteger</code> and <code>BigDecimal</code> objects. These
|
jtulach@1334
|
2891 |
* limits are stored in the superclass for serialization compatibility
|
jtulach@1334
|
2892 |
* with older versions, while the limits for <code>BigInteger</code> and
|
jtulach@1334
|
2893 |
* <code>BigDecimal</code> objects are kept in this class.
|
jtulach@1334
|
2894 |
* If, in the superclass, the minimum or maximum integer digit count is
|
jtulach@1334
|
2895 |
* larger than <code>DOUBLE_INTEGER_DIGITS</code> or if the minimum or
|
jtulach@1334
|
2896 |
* maximum fraction digit count is larger than
|
jtulach@1334
|
2897 |
* <code>DOUBLE_FRACTION_DIGITS</code>, then the stream data is invalid
|
jtulach@1334
|
2898 |
* and this method throws an <code>InvalidObjectException</code>.
|
jtulach@1334
|
2899 |
* <li>
|
jtulach@1334
|
2900 |
* If <code>serialVersionOnStream</code> is less than 4, initialize
|
jtulach@1334
|
2901 |
* <code>roundingMode</code> to {@link java.math.RoundingMode#HALF_EVEN
|
jtulach@1334
|
2902 |
* RoundingMode.HALF_EVEN}. This field is new with version 4.
|
jtulach@1334
|
2903 |
* <li>
|
jtulach@1334
|
2904 |
* If <code>serialVersionOnStream</code> is less than 3, then call
|
jtulach@1334
|
2905 |
* the setters for the minimum and maximum integer and fraction digits with
|
jtulach@1334
|
2906 |
* the values of the corresponding superclass getters to initialize the
|
jtulach@1334
|
2907 |
* fields in this class. The fields in this class are new with version 3.
|
jtulach@1334
|
2908 |
* <li>
|
jtulach@1334
|
2909 |
* If <code>serialVersionOnStream</code> is less than 1, indicating that
|
jtulach@1334
|
2910 |
* the stream was written by JDK 1.1, initialize
|
jtulach@1334
|
2911 |
* <code>useExponentialNotation</code>
|
jtulach@1334
|
2912 |
* to false, since it was not present in JDK 1.1.
|
jtulach@1334
|
2913 |
* <li>
|
jtulach@1334
|
2914 |
* Set <code>serialVersionOnStream</code> to the maximum allowed value so
|
jtulach@1334
|
2915 |
* that default serialization will work properly if this object is streamed
|
jtulach@1334
|
2916 |
* out again.
|
jtulach@1334
|
2917 |
* </ol>
|
jtulach@1334
|
2918 |
*
|
jtulach@1334
|
2919 |
* <p>Stream versions older than 2 will not have the affix pattern variables
|
jtulach@1334
|
2920 |
* <code>posPrefixPattern</code> etc. As a result, they will be initialized
|
jtulach@1334
|
2921 |
* to <code>null</code>, which means the affix strings will be taken as
|
jtulach@1334
|
2922 |
* literal values. This is exactly what we want, since that corresponds to
|
jtulach@1334
|
2923 |
* the pre-version-2 behavior.
|
jtulach@1334
|
2924 |
*/
|
jtulach@1334
|
2925 |
private void readObject(ObjectInputStream stream)
|
jtulach@1334
|
2926 |
throws IOException, ClassNotFoundException
|
jtulach@1334
|
2927 |
{
|
jtulach@1334
|
2928 |
stream.defaultReadObject();
|
jtulach@1334
|
2929 |
digitList = new DigitList();
|
jtulach@1334
|
2930 |
|
jtulach@1334
|
2931 |
if (serialVersionOnStream < 4) {
|
jtulach@1334
|
2932 |
setRoundingMode(RoundingMode.HALF_EVEN);
|
jtulach@1334
|
2933 |
}
|
jtulach@1334
|
2934 |
// We only need to check the maximum counts because NumberFormat
|
jtulach@1334
|
2935 |
// .readObject has already ensured that the maximum is greater than the
|
jtulach@1334
|
2936 |
// minimum count.
|
jtulach@1334
|
2937 |
if (super.getMaximumIntegerDigits() > DOUBLE_INTEGER_DIGITS ||
|
jtulach@1334
|
2938 |
super.getMaximumFractionDigits() > DOUBLE_FRACTION_DIGITS) {
|
jtulach@1334
|
2939 |
throw new InvalidObjectException("Digit count out of range");
|
jtulach@1334
|
2940 |
}
|
jtulach@1334
|
2941 |
if (serialVersionOnStream < 3) {
|
jtulach@1334
|
2942 |
setMaximumIntegerDigits(super.getMaximumIntegerDigits());
|
jtulach@1334
|
2943 |
setMinimumIntegerDigits(super.getMinimumIntegerDigits());
|
jtulach@1334
|
2944 |
setMaximumFractionDigits(super.getMaximumFractionDigits());
|
jtulach@1334
|
2945 |
setMinimumFractionDigits(super.getMinimumFractionDigits());
|
jtulach@1334
|
2946 |
}
|
jtulach@1334
|
2947 |
if (serialVersionOnStream < 1) {
|
jtulach@1334
|
2948 |
// Didn't have exponential fields
|
jtulach@1334
|
2949 |
useExponentialNotation = false;
|
jtulach@1334
|
2950 |
}
|
jtulach@1334
|
2951 |
serialVersionOnStream = currentSerialVersion;
|
jtulach@1334
|
2952 |
}
|
jtulach@1334
|
2953 |
|
jtulach@1334
|
2954 |
//----------------------------------------------------------------------
|
jtulach@1334
|
2955 |
// INSTANCE VARIABLES
|
jtulach@1334
|
2956 |
//----------------------------------------------------------------------
|
jtulach@1334
|
2957 |
|
jtulach@1334
|
2958 |
private transient DigitList digitList = new DigitList();
|
jtulach@1334
|
2959 |
|
jtulach@1334
|
2960 |
/**
|
jtulach@1334
|
2961 |
* The symbol used as a prefix when formatting positive numbers, e.g. "+".
|
jtulach@1334
|
2962 |
*
|
jtulach@1334
|
2963 |
* @serial
|
jtulach@1334
|
2964 |
* @see #getPositivePrefix
|
jtulach@1334
|
2965 |
*/
|
jtulach@1334
|
2966 |
private String positivePrefix = "";
|
jtulach@1334
|
2967 |
|
jtulach@1334
|
2968 |
/**
|
jtulach@1334
|
2969 |
* The symbol used as a suffix when formatting positive numbers.
|
jtulach@1334
|
2970 |
* This is often an empty string.
|
jtulach@1334
|
2971 |
*
|
jtulach@1334
|
2972 |
* @serial
|
jtulach@1334
|
2973 |
* @see #getPositiveSuffix
|
jtulach@1334
|
2974 |
*/
|
jtulach@1334
|
2975 |
private String positiveSuffix = "";
|
jtulach@1334
|
2976 |
|
jtulach@1334
|
2977 |
/**
|
jtulach@1334
|
2978 |
* The symbol used as a prefix when formatting negative numbers, e.g. "-".
|
jtulach@1334
|
2979 |
*
|
jtulach@1334
|
2980 |
* @serial
|
jtulach@1334
|
2981 |
* @see #getNegativePrefix
|
jtulach@1334
|
2982 |
*/
|
jtulach@1334
|
2983 |
private String negativePrefix = "-";
|
jtulach@1334
|
2984 |
|
jtulach@1334
|
2985 |
/**
|
jtulach@1334
|
2986 |
* The symbol used as a suffix when formatting negative numbers.
|
jtulach@1334
|
2987 |
* This is often an empty string.
|
jtulach@1334
|
2988 |
*
|
jtulach@1334
|
2989 |
* @serial
|
jtulach@1334
|
2990 |
* @see #getNegativeSuffix
|
jtulach@1334
|
2991 |
*/
|
jtulach@1334
|
2992 |
private String negativeSuffix = "";
|
jtulach@1334
|
2993 |
|
jtulach@1334
|
2994 |
/**
|
jtulach@1334
|
2995 |
* The prefix pattern for non-negative numbers. This variable corresponds
|
jtulach@1334
|
2996 |
* to <code>positivePrefix</code>.
|
jtulach@1334
|
2997 |
*
|
jtulach@1334
|
2998 |
* <p>This pattern is expanded by the method <code>expandAffix()</code> to
|
jtulach@1334
|
2999 |
* <code>positivePrefix</code> to update the latter to reflect changes in
|
jtulach@1334
|
3000 |
* <code>symbols</code>. If this variable is <code>null</code> then
|
jtulach@1334
|
3001 |
* <code>positivePrefix</code> is taken as a literal value that does not
|
jtulach@1334
|
3002 |
* change when <code>symbols</code> changes. This variable is always
|
jtulach@1334
|
3003 |
* <code>null</code> for <code>DecimalFormat</code> objects older than
|
jtulach@1334
|
3004 |
* stream version 2 restored from stream.
|
jtulach@1334
|
3005 |
*
|
jtulach@1334
|
3006 |
* @serial
|
jtulach@1334
|
3007 |
* @since 1.3
|
jtulach@1334
|
3008 |
*/
|
jtulach@1334
|
3009 |
private String posPrefixPattern;
|
jtulach@1334
|
3010 |
|
jtulach@1334
|
3011 |
/**
|
jtulach@1334
|
3012 |
* The suffix pattern for non-negative numbers. This variable corresponds
|
jtulach@1334
|
3013 |
* to <code>positiveSuffix</code>. This variable is analogous to
|
jtulach@1334
|
3014 |
* <code>posPrefixPattern</code>; see that variable for further
|
jtulach@1334
|
3015 |
* documentation.
|
jtulach@1334
|
3016 |
*
|
jtulach@1334
|
3017 |
* @serial
|
jtulach@1334
|
3018 |
* @since 1.3
|
jtulach@1334
|
3019 |
*/
|
jtulach@1334
|
3020 |
private String posSuffixPattern;
|
jtulach@1334
|
3021 |
|
jtulach@1334
|
3022 |
/**
|
jtulach@1334
|
3023 |
* The prefix pattern for negative numbers. This variable corresponds
|
jtulach@1334
|
3024 |
* to <code>negativePrefix</code>. This variable is analogous to
|
jtulach@1334
|
3025 |
* <code>posPrefixPattern</code>; see that variable for further
|
jtulach@1334
|
3026 |
* documentation.
|
jtulach@1334
|
3027 |
*
|
jtulach@1334
|
3028 |
* @serial
|
jtulach@1334
|
3029 |
* @since 1.3
|
jtulach@1334
|
3030 |
*/
|
jtulach@1334
|
3031 |
private String negPrefixPattern;
|
jtulach@1334
|
3032 |
|
jtulach@1334
|
3033 |
/**
|
jtulach@1334
|
3034 |
* The suffix pattern for negative numbers. This variable corresponds
|
jtulach@1334
|
3035 |
* to <code>negativeSuffix</code>. This variable is analogous to
|
jtulach@1334
|
3036 |
* <code>posPrefixPattern</code>; see that variable for further
|
jtulach@1334
|
3037 |
* documentation.
|
jtulach@1334
|
3038 |
*
|
jtulach@1334
|
3039 |
* @serial
|
jtulach@1334
|
3040 |
* @since 1.3
|
jtulach@1334
|
3041 |
*/
|
jtulach@1334
|
3042 |
private String negSuffixPattern;
|
jtulach@1334
|
3043 |
|
jtulach@1334
|
3044 |
/**
|
jtulach@1334
|
3045 |
* The multiplier for use in percent, per mille, etc.
|
jtulach@1334
|
3046 |
*
|
jtulach@1334
|
3047 |
* @serial
|
jtulach@1334
|
3048 |
* @see #getMultiplier
|
jtulach@1334
|
3049 |
*/
|
jtulach@1334
|
3050 |
private int multiplier = 1;
|
jtulach@1334
|
3051 |
|
jtulach@1334
|
3052 |
/**
|
jtulach@1334
|
3053 |
* The number of digits between grouping separators in the integer
|
jtulach@1334
|
3054 |
* portion of a number. Must be greater than 0 if
|
jtulach@1334
|
3055 |
* <code>NumberFormat.groupingUsed</code> is true.
|
jtulach@1334
|
3056 |
*
|
jtulach@1334
|
3057 |
* @serial
|
jtulach@1334
|
3058 |
* @see #getGroupingSize
|
jtulach@1334
|
3059 |
* @see java.text.NumberFormat#isGroupingUsed
|
jtulach@1334
|
3060 |
*/
|
jtulach@1334
|
3061 |
private byte groupingSize = 3; // invariant, > 0 if useThousands
|
jtulach@1334
|
3062 |
|
jtulach@1334
|
3063 |
/**
|
jtulach@1334
|
3064 |
* If true, forces the decimal separator to always appear in a formatted
|
jtulach@1334
|
3065 |
* number, even if the fractional part of the number is zero.
|
jtulach@1334
|
3066 |
*
|
jtulach@1334
|
3067 |
* @serial
|
jtulach@1334
|
3068 |
* @see #isDecimalSeparatorAlwaysShown
|
jtulach@1334
|
3069 |
*/
|
jtulach@1334
|
3070 |
private boolean decimalSeparatorAlwaysShown = false;
|
jtulach@1334
|
3071 |
|
jtulach@1334
|
3072 |
/**
|
jtulach@1334
|
3073 |
* If true, parse returns BigDecimal wherever possible.
|
jtulach@1334
|
3074 |
*
|
jtulach@1334
|
3075 |
* @serial
|
jtulach@1334
|
3076 |
* @see #isParseBigDecimal
|
jtulach@1334
|
3077 |
* @since 1.5
|
jtulach@1334
|
3078 |
*/
|
jtulach@1334
|
3079 |
private boolean parseBigDecimal = false;
|
jtulach@1334
|
3080 |
|
jtulach@1334
|
3081 |
|
jtulach@1334
|
3082 |
/**
|
jtulach@1334
|
3083 |
* True if this object represents a currency format. This determines
|
jtulach@1334
|
3084 |
* whether the monetary decimal separator is used instead of the normal one.
|
jtulach@1334
|
3085 |
*/
|
jtulach@1334
|
3086 |
private transient boolean isCurrencyFormat = false;
|
jtulach@1334
|
3087 |
|
jtulach@1334
|
3088 |
/**
|
jtulach@1334
|
3089 |
* The <code>DecimalFormatSymbols</code> object used by this format.
|
jtulach@1334
|
3090 |
* It contains the symbols used to format numbers, e.g. the grouping separator,
|
jtulach@1334
|
3091 |
* decimal separator, and so on.
|
jtulach@1334
|
3092 |
*
|
jtulach@1334
|
3093 |
* @serial
|
jtulach@1334
|
3094 |
* @see #setDecimalFormatSymbols
|
jtulach@1334
|
3095 |
* @see java.text.DecimalFormatSymbols
|
jtulach@1334
|
3096 |
*/
|
jtulach@1334
|
3097 |
private DecimalFormatSymbols symbols = null; // LIU new DecimalFormatSymbols();
|
jtulach@1334
|
3098 |
|
jtulach@1334
|
3099 |
/**
|
jtulach@1334
|
3100 |
* True to force the use of exponential (i.e. scientific) notation when formatting
|
jtulach@1334
|
3101 |
* numbers.
|
jtulach@1334
|
3102 |
*
|
jtulach@1334
|
3103 |
* @serial
|
jtulach@1334
|
3104 |
* @since 1.2
|
jtulach@1334
|
3105 |
*/
|
jtulach@1334
|
3106 |
private boolean useExponentialNotation; // Newly persistent in the Java 2 platform v.1.2
|
jtulach@1334
|
3107 |
|
jtulach@1334
|
3108 |
/**
|
jtulach@1334
|
3109 |
* FieldPositions describing the positive prefix String. This is
|
jtulach@1334
|
3110 |
* lazily created. Use <code>getPositivePrefixFieldPositions</code>
|
jtulach@1334
|
3111 |
* when needed.
|
jtulach@1334
|
3112 |
*/
|
jtulach@1334
|
3113 |
private transient FieldPosition[] positivePrefixFieldPositions;
|
jtulach@1334
|
3114 |
|
jtulach@1334
|
3115 |
/**
|
jtulach@1334
|
3116 |
* FieldPositions describing the positive suffix String. This is
|
jtulach@1334
|
3117 |
* lazily created. Use <code>getPositiveSuffixFieldPositions</code>
|
jtulach@1334
|
3118 |
* when needed.
|
jtulach@1334
|
3119 |
*/
|
jtulach@1334
|
3120 |
private transient FieldPosition[] positiveSuffixFieldPositions;
|
jtulach@1334
|
3121 |
|
jtulach@1334
|
3122 |
/**
|
jtulach@1334
|
3123 |
* FieldPositions describing the negative prefix String. This is
|
jtulach@1334
|
3124 |
* lazily created. Use <code>getNegativePrefixFieldPositions</code>
|
jtulach@1334
|
3125 |
* when needed.
|
jtulach@1334
|
3126 |
*/
|
jtulach@1334
|
3127 |
private transient FieldPosition[] negativePrefixFieldPositions;
|
jtulach@1334
|
3128 |
|
jtulach@1334
|
3129 |
/**
|
jtulach@1334
|
3130 |
* FieldPositions describing the negative suffix String. This is
|
jtulach@1334
|
3131 |
* lazily created. Use <code>getNegativeSuffixFieldPositions</code>
|
jtulach@1334
|
3132 |
* when needed.
|
jtulach@1334
|
3133 |
*/
|
jtulach@1334
|
3134 |
private transient FieldPosition[] negativeSuffixFieldPositions;
|
jtulach@1334
|
3135 |
|
jtulach@1334
|
3136 |
/**
|
jtulach@1334
|
3137 |
* The minimum number of digits used to display the exponent when a number is
|
jtulach@1334
|
3138 |
* formatted in exponential notation. This field is ignored if
|
jtulach@1334
|
3139 |
* <code>useExponentialNotation</code> is not true.
|
jtulach@1334
|
3140 |
*
|
jtulach@1334
|
3141 |
* @serial
|
jtulach@1334
|
3142 |
* @since 1.2
|
jtulach@1334
|
3143 |
*/
|
jtulach@1334
|
3144 |
private byte minExponentDigits; // Newly persistent in the Java 2 platform v.1.2
|
jtulach@1334
|
3145 |
|
jtulach@1334
|
3146 |
/**
|
jtulach@1334
|
3147 |
* The maximum number of digits allowed in the integer portion of a
|
jtulach@1334
|
3148 |
* <code>BigInteger</code> or <code>BigDecimal</code> number.
|
jtulach@1334
|
3149 |
* <code>maximumIntegerDigits</code> must be greater than or equal to
|
jtulach@1334
|
3150 |
* <code>minimumIntegerDigits</code>.
|
jtulach@1334
|
3151 |
*
|
jtulach@1334
|
3152 |
* @serial
|
jtulach@1334
|
3153 |
* @see #getMaximumIntegerDigits
|
jtulach@1334
|
3154 |
* @since 1.5
|
jtulach@1334
|
3155 |
*/
|
jtulach@1334
|
3156 |
private int maximumIntegerDigits = super.getMaximumIntegerDigits();
|
jtulach@1334
|
3157 |
|
jtulach@1334
|
3158 |
/**
|
jtulach@1334
|
3159 |
* The minimum number of digits allowed in the integer portion of a
|
jtulach@1334
|
3160 |
* <code>BigInteger</code> or <code>BigDecimal</code> number.
|
jtulach@1334
|
3161 |
* <code>minimumIntegerDigits</code> must be less than or equal to
|
jtulach@1334
|
3162 |
* <code>maximumIntegerDigits</code>.
|
jtulach@1334
|
3163 |
*
|
jtulach@1334
|
3164 |
* @serial
|
jtulach@1334
|
3165 |
* @see #getMinimumIntegerDigits
|
jtulach@1334
|
3166 |
* @since 1.5
|
jtulach@1334
|
3167 |
*/
|
jtulach@1334
|
3168 |
private int minimumIntegerDigits = super.getMinimumIntegerDigits();
|
jtulach@1334
|
3169 |
|
jtulach@1334
|
3170 |
/**
|
jtulach@1334
|
3171 |
* The maximum number of digits allowed in the fractional portion of a
|
jtulach@1334
|
3172 |
* <code>BigInteger</code> or <code>BigDecimal</code> number.
|
jtulach@1334
|
3173 |
* <code>maximumFractionDigits</code> must be greater than or equal to
|
jtulach@1334
|
3174 |
* <code>minimumFractionDigits</code>.
|
jtulach@1334
|
3175 |
*
|
jtulach@1334
|
3176 |
* @serial
|
jtulach@1334
|
3177 |
* @see #getMaximumFractionDigits
|
jtulach@1334
|
3178 |
* @since 1.5
|
jtulach@1334
|
3179 |
*/
|
jtulach@1334
|
3180 |
private int maximumFractionDigits = super.getMaximumFractionDigits();
|
jtulach@1334
|
3181 |
|
jtulach@1334
|
3182 |
/**
|
jtulach@1334
|
3183 |
* The minimum number of digits allowed in the fractional portion of a
|
jtulach@1334
|
3184 |
* <code>BigInteger</code> or <code>BigDecimal</code> number.
|
jtulach@1334
|
3185 |
* <code>minimumFractionDigits</code> must be less than or equal to
|
jtulach@1334
|
3186 |
* <code>maximumFractionDigits</code>.
|
jtulach@1334
|
3187 |
*
|
jtulach@1334
|
3188 |
* @serial
|
jtulach@1334
|
3189 |
* @see #getMinimumFractionDigits
|
jtulach@1334
|
3190 |
* @since 1.5
|
jtulach@1334
|
3191 |
*/
|
jtulach@1334
|
3192 |
private int minimumFractionDigits = super.getMinimumFractionDigits();
|
jtulach@1334
|
3193 |
|
jtulach@1334
|
3194 |
/**
|
jtulach@1334
|
3195 |
* The {@link java.math.RoundingMode} used in this DecimalFormat.
|
jtulach@1334
|
3196 |
*
|
jtulach@1334
|
3197 |
* @serial
|
jtulach@1334
|
3198 |
* @since 1.6
|
jtulach@1334
|
3199 |
*/
|
jtulach@1334
|
3200 |
private RoundingMode roundingMode = RoundingMode.HALF_EVEN;
|
jtulach@1334
|
3201 |
|
jtulach@1334
|
3202 |
//----------------------------------------------------------------------
|
jtulach@1334
|
3203 |
|
jtulach@1334
|
3204 |
static final int currentSerialVersion = 4;
|
jtulach@1334
|
3205 |
|
jtulach@1334
|
3206 |
/**
|
jtulach@1334
|
3207 |
* The internal serial version which says which version was written.
|
jtulach@1334
|
3208 |
* Possible values are:
|
jtulach@1334
|
3209 |
* <ul>
|
jtulach@1334
|
3210 |
* <li><b>0</b> (default): versions before the Java 2 platform v1.2
|
jtulach@1334
|
3211 |
* <li><b>1</b>: version for 1.2, which includes the two new fields
|
jtulach@1334
|
3212 |
* <code>useExponentialNotation</code> and
|
jtulach@1334
|
3213 |
* <code>minExponentDigits</code>.
|
jtulach@1334
|
3214 |
* <li><b>2</b>: version for 1.3 and later, which adds four new fields:
|
jtulach@1334
|
3215 |
* <code>posPrefixPattern</code>, <code>posSuffixPattern</code>,
|
jtulach@1334
|
3216 |
* <code>negPrefixPattern</code>, and <code>negSuffixPattern</code>.
|
jtulach@1334
|
3217 |
* <li><b>3</b>: version for 1.5 and later, which adds five new fields:
|
jtulach@1334
|
3218 |
* <code>maximumIntegerDigits</code>,
|
jtulach@1334
|
3219 |
* <code>minimumIntegerDigits</code>,
|
jtulach@1334
|
3220 |
* <code>maximumFractionDigits</code>,
|
jtulach@1334
|
3221 |
* <code>minimumFractionDigits</code>, and
|
jtulach@1334
|
3222 |
* <code>parseBigDecimal</code>.
|
jtulach@1334
|
3223 |
* <li><b>4</b>: version for 1.6 and later, which adds one new field:
|
jtulach@1334
|
3224 |
* <code>roundingMode</code>.
|
jtulach@1334
|
3225 |
* </ul>
|
jtulach@1334
|
3226 |
* @since 1.2
|
jtulach@1334
|
3227 |
* @serial
|
jtulach@1334
|
3228 |
*/
|
jtulach@1334
|
3229 |
private int serialVersionOnStream = currentSerialVersion;
|
jtulach@1334
|
3230 |
|
jtulach@1334
|
3231 |
//----------------------------------------------------------------------
|
jtulach@1334
|
3232 |
// CONSTANTS
|
jtulach@1334
|
3233 |
//----------------------------------------------------------------------
|
jtulach@1334
|
3234 |
|
jtulach@1334
|
3235 |
// Constants for characters used in programmatic (unlocalized) patterns.
|
jtulach@1334
|
3236 |
private static final char PATTERN_ZERO_DIGIT = '0';
|
jtulach@1334
|
3237 |
private static final char PATTERN_GROUPING_SEPARATOR = ',';
|
jtulach@1334
|
3238 |
private static final char PATTERN_DECIMAL_SEPARATOR = '.';
|
jtulach@1334
|
3239 |
private static final char PATTERN_PER_MILLE = '\u2030';
|
jtulach@1334
|
3240 |
private static final char PATTERN_PERCENT = '%';
|
jtulach@1334
|
3241 |
private static final char PATTERN_DIGIT = '#';
|
jtulach@1334
|
3242 |
private static final char PATTERN_SEPARATOR = ';';
|
jtulach@1334
|
3243 |
private static final String PATTERN_EXPONENT = "E";
|
jtulach@1334
|
3244 |
private static final char PATTERN_MINUS = '-';
|
jtulach@1334
|
3245 |
|
jtulach@1334
|
3246 |
/**
|
jtulach@1334
|
3247 |
* The CURRENCY_SIGN is the standard Unicode symbol for currency. It
|
jtulach@1334
|
3248 |
* is used in patterns and substituted with either the currency symbol,
|
jtulach@1334
|
3249 |
* or if it is doubled, with the international currency symbol. If the
|
jtulach@1334
|
3250 |
* CURRENCY_SIGN is seen in a pattern, then the decimal separator is
|
jtulach@1334
|
3251 |
* replaced with the monetary decimal separator.
|
jtulach@1334
|
3252 |
*
|
jtulach@1334
|
3253 |
* The CURRENCY_SIGN is not localized.
|
jtulach@1334
|
3254 |
*/
|
jtulach@1334
|
3255 |
private static final char CURRENCY_SIGN = '\u00A4';
|
jtulach@1334
|
3256 |
|
jtulach@1334
|
3257 |
private static final char QUOTE = '\'';
|
jtulach@1334
|
3258 |
|
jtulach@1334
|
3259 |
private static FieldPosition[] EmptyFieldPositionArray = new FieldPosition[0];
|
jtulach@1334
|
3260 |
|
jtulach@1334
|
3261 |
// Upper limit on integer and fraction digits for a Java double
|
jtulach@1334
|
3262 |
static final int DOUBLE_INTEGER_DIGITS = 309;
|
jtulach@1334
|
3263 |
static final int DOUBLE_FRACTION_DIGITS = 340;
|
jtulach@1334
|
3264 |
|
jtulach@1334
|
3265 |
// Upper limit on integer and fraction digits for BigDecimal and BigInteger
|
jtulach@1334
|
3266 |
static final int MAXIMUM_INTEGER_DIGITS = Integer.MAX_VALUE;
|
jtulach@1334
|
3267 |
static final int MAXIMUM_FRACTION_DIGITS = Integer.MAX_VALUE;
|
jtulach@1334
|
3268 |
|
jtulach@1334
|
3269 |
// Proclaim JDK 1.1 serial compatibility.
|
jtulach@1334
|
3270 |
static final long serialVersionUID = 864413376551465018L;
|
jtulach@1334
|
3271 |
|
jtulach@1334
|
3272 |
/**
|
jtulach@1334
|
3273 |
* Cache to hold the NumberPattern of a Locale.
|
jtulach@1334
|
3274 |
*/
|
jtulach@1334
|
3275 |
private static final ConcurrentMap<Locale, String> cachedLocaleData
|
jtulach@1334
|
3276 |
= new ConcurrentHashMap<Locale, String>(3);
|
jtulach@1334
|
3277 |
}
|