1 /*
2  * Copyright (c) 2003, 2016, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.
8  *
9  * This code is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * version 2 for more details (a copy is included in the LICENSE file that
13  * accompanied this code).
14  *
15  * You should have received a copy of the GNU General Public License version
16  * 2 along with this work; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20  * or visit www.oracle.com if you need additional information or have any
21  * questions.
22  */
23 
24 /*
25  * @test
26  * @bug 4018937 8008577
27  * @summary Confirm that methods which are newly added to support BigDecimal and BigInteger work as expected.
28  * @library /java/text/testlib
29  * @run main/othervm -Djava.locale.providers=COMPAT,SPI BigDecimalFormat
30  */
31 
32 import java.math.BigDecimal;
33 import java.math.BigInteger;
34 import java.text.*;
35 import java.util.*;
36 
37 public class BigDecimalFormat extends IntlTest {
38 
main(String[] args)39     public static void main(String[] args) throws Exception {
40         new BigDecimalFormat().run(args);
41     }
42 
43     static final String nonsep_int =
44         "123456789012345678901234567890123456789012345678901234567890" +
45         "123456789012345678901234567890123456789012345678901234567890" +
46         "123456789012345678901234567890123456789012345678901234567890" +
47         "123456789012345678901234567890123456789012345678901234567890" +
48         "123456789012345678901234567890123456789012345678901234567890" +
49         "123456789012345678901234567890123456789012345678901234567890";
50 
51     static final String sep_int =
52         "123,456,789,012,345,678,901,234,567,890," +
53         "123,456,789,012,345,678,901,234,567,890," +
54         "123,456,789,012,345,678,901,234,567,890," +
55         "123,456,789,012,345,678,901,234,567,890," +
56         "123,456,789,012,345,678,901,234,567,890," +
57         "123,456,789,012,345,678,901,234,567,890," +
58         "123,456,789,012,345,678,901,234,567,890," +
59         "123,456,789,012,345,678,901,234,567,890," +
60         "123,456,789,012,345,678,901,234,567,890," +
61         "123,456,789,012,345,678,901,234,567,890," +
62         "123,456,789,012,345,678,901,234,567,890," +
63         "123,456,789,012,345,678,901,234,567,890";
64 
65     static final String nonsep_zero =
66         "000000000000000000000000000000000000000000000000000000000000" +
67         "000000000000000000000000000000000000000000000000000000000000" +
68         "000000000000000000000000000000000000000000000000000000000000" +
69         "000000000000000000000000000000000000000000000000000000000000" +
70         "000000000000000000000000000000000000000000000000000000000000" +
71         "000000000000000000000000000000000000000000000000000000000000";
72 
73     static final String sep_zero =
74         "000,000,000,000,000,000,000,000,000,000," +
75         "000,000,000,000,000,000,000,000,000,000," +
76         "000,000,000,000,000,000,000,000,000,000," +
77         "000,000,000,000,000,000,000,000,000,000," +
78         "000,000,000,000,000,000,000,000,000,000," +
79         "000,000,000,000,000,000,000,000,000,000," +
80         "000,000,000,000,000,000,000,000,000,000," +
81         "000,000,000,000,000,000,000,000,000,000," +
82         "000,000,000,000,000,000,000,000,000,000," +
83         "000,000,000,000,000,000,000,000,000,000," +
84         "000,000,000,000,000,000,000,000,000,000," +
85         "000,000,000,000,000,000,000,000,000,000";
86 
87     static final String fra =
88         "012345678901234567890123456789012345678901234567890123456789" +
89         "012345678901234567890123456789012345678901234567890123456789" +
90         "012345678901234567890123456789012345678901234567890123456789" +
91         "012345678901234567890123456789012345678901234567890123456789" +
92         "012345678901234567890123456789012345678901234567890123456789" +
93         "012345678901234567890123456789012345678901234567890123456789";
94 
95 
96     StringBuffer formatted = new StringBuffer(1000);
97     FieldPosition fp;
98 
99     /**
100      * Test for normal big numbers which have the fraction part
101      */
test_Format_in_NumberFormat_BigDecimal()102     void test_Format_in_NumberFormat_BigDecimal() {
103         String from, to;
104 
105         NumberFormat nf = NumberFormat.getInstance(Locale.US);
106         ((DecimalFormat)nf).applyPattern("#,##0.###");
107         setDigits(nf, Integer.MAX_VALUE, 1, Integer.MAX_VALUE, 0);
108 
109         // From: 0.000...789
110         // To:   0.000...789 (same as From)
111         formatted.setLength(0);
112         from = "0." + nonsep_zero + "123456789";
113         nf.format(new BigDecimal(from), formatted, new FieldPosition(0));
114         checkFormat(from, formatted, from, ((DecimalFormat)nf).getMultiplier());
115 
116         /* ------------------------------------------------------------------ */
117 
118         // From: -0.000...789
119         // To:   -0.000...789 (same as From)
120         //       ~ : FieldPosition(SIGN)
121         fp = new FieldPosition(NumberFormat.Field.SIGN);
122         formatted.setLength(0);
123         from = "-0." + nonsep_zero + "123456789";
124         nf.format(new BigDecimal(from), formatted, fp);
125         checkFormat(from, formatted, from, ((DecimalFormat)nf).getMultiplier());
126         checkFieldPosition(from, fp, 0, 1);
127 
128         /* ------------------------------------------------------------------ */
129 
130         // From: 1234...7890.012...789
131         // To:   123,4...7,890.012...789
132         //       ~~~~~~~~~~~~~ : FieldPosition(INTEGER_FIELD)
133         fp = new FieldPosition(DecimalFormat.INTEGER_FIELD);
134         formatted.setLength(0);
135         from = nonsep_int + "." + fra;
136         to   = sep_int    + "." + fra;
137         nf.format(new BigDecimal(from), formatted, fp);
138         checkFormat(from, formatted, to, ((DecimalFormat)nf).getMultiplier());
139         checkFieldPosition(from, fp, 0, 479);
140 
141         /* ------------------------------------------------------------------ */
142 
143         // From: -1234...7890.012...789
144         // To:   -123,4...7,890.012...789
145         //                    ~~~~~~~~~ : FieldPosition(FRACTION_FIELD)
146         fp = new FieldPosition(DecimalFormat.FRACTION_FIELD);
147         formatted.setLength(0);
148         from = "-" + nonsep_int + "." + fra;
149         to   = "-" + sep_int    + "." + fra;
150         nf.format(new BigDecimal(from), formatted, fp);
151         checkFormat(from, formatted, to, ((DecimalFormat)nf).getMultiplier());
152         checkFieldPosition(from, fp, 481, 841);
153 
154         /* ------------------------------------------------------------------ */
155 
156         // From: 1234...78900000...0000.000...789
157         // To:   123,4...7,890,000,0...0,000.000...789
158         formatted.setLength(0);
159         from = nonsep_int + nonsep_zero + "." + nonsep_zero + fra;
160         to   = sep_int + "," + sep_zero + "." + nonsep_zero + fra;
161         nf.format(new BigDecimal(from), formatted, new FieldPosition(0));
162         checkFormat(from, formatted, to, ((DecimalFormat)nf).getMultiplier());
163 
164         /* ------------------------------------------------------------------ */
165 
166         // From: -1234...78900000...0000.000...789
167         // To:   -123,4...7,890,000,0...0,000.000...789
168         formatted.setLength(0);
169         from = "-" + nonsep_int + nonsep_zero + "." + nonsep_zero + fra;
170         to   = "-" + sep_int + "," + sep_zero + "." + nonsep_zero + fra;
171         nf.format(new BigDecimal(from), formatted, new FieldPosition(0));
172         checkFormat(from, formatted, to, ((DecimalFormat)nf).getMultiplier());
173 
174         /* ------------------------------------------------------------------ */
175 
176         // From: 1234...78900000...0000
177         // To:   123,4...7,890,000,0...0,000
178         formatted.setLength(0);
179         from = nonsep_int + nonsep_zero;
180         to   = sep_int + "," + sep_zero;
181         nf.format(new BigDecimal(from), formatted, new FieldPosition(0));
182         checkFormat(from, formatted, to, ((DecimalFormat)nf).getMultiplier());
183 
184         /* ------------------------------------------------------------------ */
185 
186         // From: -1234...78900000...0000
187         // To:   -123,4...7,890,000,0...0,000
188         formatted.setLength(0);
189         from = "-" + nonsep_int + nonsep_zero;
190         to   = "-" + sep_int + "," + sep_zero;
191         nf.format(new BigDecimal(from), formatted, new FieldPosition(0));
192         checkFormat(from, formatted, to, ((DecimalFormat)nf).getMultiplier());
193 
194         /* ------------------------------------------------------------------ */
195 
196         // From: 1234...78900000...0000.0...0
197         // To:   1,234...7,890,000,0...0,000
198         formatted.setLength(0);
199         from = nonsep_int + nonsep_zero + "." + nonsep_zero;
200         to   = sep_int + "," + sep_zero;
201         nf.format(new BigDecimal(from), formatted, new FieldPosition(0));
202         checkFormat(from, formatted, to, ((DecimalFormat)nf).getMultiplier());
203 
204         /* ------------------------------------------------------------------ */
205 
206         // From: -1234...78900000...0000.0...0
207         // To:   -1,234...7,890,000,0...0,000
208         formatted.setLength(0);
209         from = "-" + nonsep_int + nonsep_zero + "." + nonsep_zero;
210         to   = "-" + sep_int + "," + sep_zero;
211         nf.format(new BigDecimal(from), formatted, new FieldPosition(0));
212         checkFormat(from, formatted, to, ((DecimalFormat)nf).getMultiplier());
213 
214         /* ------------------------------------------------------------------ */
215 
216         // From: 000...0000
217         // To:   0
218         formatted.setLength(0);
219         from = nonsep_zero;
220         to   = "0";
221         nf.format(new BigDecimal(from), formatted, new FieldPosition(0));
222         checkFormat(from, formatted, to, ((DecimalFormat)nf).getMultiplier());
223 
224         /* ------------------------------------------------------------------ */
225 
226         // From: -000...0000
227         // To:   0
228         formatted.setLength(0);
229         from = "-" + nonsep_zero;
230         to   = "0";
231         nf.format(new BigDecimal(from), formatted, new FieldPosition(0));
232         checkFormat(from, formatted, to, ((DecimalFormat)nf).getMultiplier());
233 
234         /* ------------------------------------------------------------------ */
235 
236         // From: 000...00001234
237         // To:   1,234
238         formatted.setLength(0);
239         from = nonsep_zero + "1234";
240         to   = "1,234";
241         nf.format(new BigDecimal(from), formatted, new FieldPosition(0));
242         checkFormat(from, formatted, to, ((DecimalFormat)nf).getMultiplier());
243 
244         /* ------------------------------------------------------------------ */
245 
246         // From: -000...00001234
247         // To:   -1,234
248         //       ~ : FieldPosition(GROUPING_SEPARATOR)
249         fp = new FieldPosition(NumberFormat.Field.GROUPING_SEPARATOR);
250         formatted.setLength(0);
251         from = "-" + nonsep_zero + "1234";
252         to   = "-1,234";
253         nf.format(new BigDecimal(from), formatted, fp);
254         checkFormat(from, formatted, to, ((DecimalFormat)nf).getMultiplier());
255         checkFieldPosition(from, fp, 2, 3);
256 
257         /* ------------------------------------------------------------------ */
258 
259         // From: 000...0000.0...0
260         // To:   0
261         formatted.setLength(0);
262         from = nonsep_zero + "." + nonsep_zero;
263         to   = "0";
264         nf.format(new BigDecimal(from), formatted, new FieldPosition(0));
265         checkFormat(from, formatted, to, ((DecimalFormat)nf).getMultiplier());
266 
267         /* ------------------------------------------------------------------ */
268 
269         ((DecimalFormat)nf).applyPattern("#,##0.0");
270         setDigits(nf, Integer.MAX_VALUE, 1, Integer.MAX_VALUE, 1);
271 
272         // From: -000...0000.0...0
273         // To:   0.0
274         formatted.setLength(0);
275         from = "-" + nonsep_zero + "." + nonsep_zero;
276         to   = "0.0";
277         nf.format(new BigDecimal(from), formatted, new FieldPosition(0));
278         checkFormat(from, formatted, to, ((DecimalFormat)nf).getMultiplier());
279 
280         /* ------------------------------------------------------------------ */
281 
282         // From: 1234...7890.012...7890...0
283         // To:   1,234...7,890.0123...789
284         formatted.setLength(0);
285         from = nonsep_int + "." + fra + nonsep_zero;
286         to   = sep_int    + "." + fra;
287         nf.format(new BigDecimal(from), formatted, new FieldPosition(0));
288         checkFormat(from, formatted, to, ((DecimalFormat)nf).getMultiplier());
289 
290         /* ------------------------------------------------------------------ */
291 
292         // From: -1234...7890.012...7890...0
293         // To:   -1,234...7,890.0123...789
294         formatted.setLength(0);
295         from = "-" + nonsep_int + "." + fra + nonsep_zero;
296         to   = "-" + sep_int    + "." + fra;
297         nf.format(new BigDecimal(from), formatted, new FieldPosition(0));
298         checkFormat(from, formatted, to, ((DecimalFormat)nf).getMultiplier());
299 
300         /* ------------------------------------------------------------------ */
301 
302         ((DecimalFormat)nf).applyPattern("0.###E0");
303         setDigits(nf, 1, 1, Integer.MAX_VALUE, 0);
304 
305         // From: 1123...890.012...789
306         // To  : 1.123...8900123...789E360
307         //                           ~~~ : FieldPosition(EXPONENT)
308         fp = new FieldPosition(NumberFormat.Field.EXPONENT);
309         formatted.setLength(0);
310         from = "1"  + nonsep_int + "." + fra;
311         to   = "1." + nonsep_int       + fra + "E360";
312         nf.format(new BigDecimal(from), formatted, fp);
313         checkFormat(from, formatted, to, ((DecimalFormat)nf).getMultiplier());
314         checkFieldPosition(from, fp, 723, 726);
315 
316         /* ------------------------------------------------------------------ */
317 
318         // From: -1123...890.012...789
319         // To  : -1.123...8900123...789E360
320         formatted.setLength(0);
321         from = "-1"  + nonsep_int + "." + fra;
322         to   = "-1." + nonsep_int       + fra + "E360";
323         nf.format(new BigDecimal(from), formatted, new FieldPosition(0));
324         checkFormat(from, formatted, to, ((DecimalFormat)nf).getMultiplier());
325 
326         /* ------------------------------------------------------------------ */
327 
328         ((DecimalFormat)nf).applyPattern("0.###E0");
329         setDigits(nf, 1, 1, Integer.MAX_VALUE, 0);
330 
331         // From: 0.000...0001123...890.012...789
332         // To  : 1.123...8900123...789E-360
333         formatted.setLength(0);
334         from = "0." + nonsep_zero + "1" + fra;
335         to   = "1."                  + fra + "E-361";
336         nf.format(new BigDecimal(from), formatted, new FieldPosition(0));
337         checkFormat(from, formatted, to, ((DecimalFormat)nf).getMultiplier());
338 
339         /* ------------------------------------------------------------------ */
340 
341         // From: -0.000...0001123...890.012...789
342         // To  : -1.123...8900123...789E-360
343         formatted.setLength(0);
344         from = "-0." + nonsep_zero + "1"  + fra;
345         to   = "-1."                  + fra + "E-361";
346         nf.format(new BigDecimal(from), formatted, new FieldPosition(0));
347         checkFormat(from, formatted, to, ((DecimalFormat)nf).getMultiplier());
348 
349         /* ------------------------------------------------------------------ */
350 
351         // From: 1123...890.012...789000...000
352         // To  : 1.123...8900123...789E360
353         formatted.setLength(0);
354         from = "1"  + nonsep_int + "." + fra + nonsep_zero;
355         to   = "1." + nonsep_int       + fra + "E360";
356         nf.format(new BigDecimal(from), formatted, new FieldPosition(0));
357         checkFormat(from, formatted, to, ((DecimalFormat)nf).getMultiplier());
358 
359         /* ------------------------------------------------------------------ */
360 
361         // From: -1123...890.012...789000...000
362         // To  : -1.123...8900123...789E360
363         //                           ~ : FieldPosition(EXPONENT_SYMBOL)
364         fp = new FieldPosition(NumberFormat.Field.EXPONENT_SYMBOL);
365         formatted.setLength(0);
366         from = "-1"  + nonsep_int + "." + fra + nonsep_zero;
367         to   = "-1." + nonsep_int       + fra + "E360";
368         nf.format(new BigDecimal(from), formatted, fp);
369         checkFormat(from, formatted, to, ((DecimalFormat)nf).getMultiplier());
370         checkFieldPosition(from, fp, 723, 724);
371 
372         /* ------------------------------------------------------------------ */
373 
374         // From: 0.000...0001123...890.012...789000...000
375         // To  : 1.123...8900123...789E-360
376         //                           ~ : FieldPosition(EXPONENT_SIGN)
377         fp = new FieldPosition(NumberFormat.Field.EXPONENT_SIGN);
378         formatted.setLength(0);
379         from = "0." + nonsep_zero + "1" + fra + nonsep_zero;
380         to   = "1."                  + fra + "E-361";
381         nf.format(new BigDecimal(from), formatted, fp);
382         checkFormat(from, formatted, to, ((DecimalFormat)nf).getMultiplier());
383         checkFieldPosition(from, fp, 363, 364);
384 
385         /* ------------------------------------------------------------------ */
386 
387         // From: -0.000...0001123...890.012...789000...000
388         // To  : -1.123...8900123...789E-360
389         formatted.setLength(0);
390         from = "-0." + nonsep_zero + "1"  + fra + nonsep_zero;
391         to   = "-1."                  + fra + "E-361";
392         nf.format(new BigDecimal(from), formatted, new FieldPosition(0));
393         checkFormat(from, formatted, to, ((DecimalFormat)nf).getMultiplier());
394 
395         /* ------------------------------------------------------------------ */
396 
397         // From: ABC1123...890.012...789
398         // To  : ABC1.123...890.0123...789
399         formatted = new StringBuffer("ABC");
400         from = "1"     + nonsep_int + "."  + fra;
401         to   = "ABC1." + nonsep_int     + fra + "E360";
402         nf.format(new BigDecimal(from), formatted, new FieldPosition(0));
403         checkFormat(from, formatted, to, ((DecimalFormat)nf).getMultiplier());
404 
405         /* ------------------------------------------------------------------ */
406 
407         // From: ABC-1123...890.012...789
408         // To  : ABC-1.123...890.0123...789
409         //          ~ : FieldPosition(DECIMAL_SEPARATOR)
410         fp = new FieldPosition(NumberFormat.Field.DECIMAL_SEPARATOR);
411         formatted = new StringBuffer("ABC");
412         from = "-1"     + nonsep_int + "."  + fra;
413         to   = "ABC-1." + nonsep_int    + fra + "E360";
414         nf.format(new BigDecimal(from), formatted, fp);
415         checkFormat(from, formatted, to, ((DecimalFormat)nf).getMultiplier());
416         checkFieldPosition(from, fp, 5, 6);
417 
418         /* ------------------------------------------------------------------ */
419 
420         ((DecimalFormat)nf).applyPattern("#,##0.###");
421         setDigits(nf, Integer.MAX_VALUE, 1, 726, 0);
422 
423         // From: 0.000...000012...7890123456789
424         // To:   0.000...000012...789012346 (Shorter than From)
425         formatted.setLength(0);
426         from = "0." + nonsep_zero + fra + fra;
427         to   = "0." + nonsep_zero + fra + "012346";
428         nf.format(new BigDecimal(from), formatted, new FieldPosition(0));
429         checkFormat(from, formatted, to, ((DecimalFormat)nf).getMultiplier());
430 
431         ((DecimalFormat)nf).applyPattern("#,##0.###");
432         setDigits(nf, Integer.MAX_VALUE, 1, 723, 0);
433 
434         /* ------------------------------------------------------------------ */
435 
436         // From: -0.000...000012...7890123456789
437         // To:   -0.000...000012...789012 (Shorter than From)
438         formatted.setLength(0);
439         from = "-0." + nonsep_zero + fra + fra;
440         to   = "-0." + nonsep_zero + fra + "012";
441         nf.format(new BigDecimal(from), formatted, new FieldPosition(0));
442         checkFormat(from, formatted, to, ((DecimalFormat)nf).getMultiplier());
443 
444         /* ------------------------------------------------------------------ */
445 
446         ((DecimalFormat)nf).applyPattern("00000.###E0");
447         setDigits(nf, 5, 5, 370, 0);
448 
449         // From: 1234567890.012...78901234567890
450         // To:   12345.67890012...789012346E5
451         formatted.setLength(0);
452         from = "1234567890." + fra + "0123456789";
453         to   = "12345.67890" + fra + "01235E5";
454         nf.format(new BigDecimal(from), formatted, new FieldPosition(0));
455         checkFormat(from, formatted, to, ((DecimalFormat)nf).getMultiplier());
456 
457         /* ------------------------------------------------------------------ */
458 
459         ((DecimalFormat)nf).applyPattern("0.###E0");
460         setDigits(nf, 1, 1, 364, 0);
461 
462         // From: -0.000...0001012...7890123456789
463         // To:   -1.012...789012E-361
464         formatted.setLength(0);
465         from = "-0." + nonsep_zero + "1" + fra + "0123456789";
466         to   = "-1."                 + fra + "0123E-361";
467         nf.format(new BigDecimal(from), formatted, new FieldPosition(0));
468         checkFormat(from, formatted, to, ((DecimalFormat)nf).getMultiplier());
469 
470         /* ------------------------------------------------------------------ */
471 
472         ((DecimalFormat)nf).applyPattern("0.###E0");
473         setDigits(nf, 1, 1, 366, 0);
474 
475         // From: 1012...78901234567890
476         // To:   1.012...789012346E370
477         formatted.setLength(0);
478         from = "1"  + fra + "0123456789";
479         to   = "1." + fra + "012346E370";
480         nf.format(new BigDecimal(from), formatted, new FieldPosition(0));
481         checkFormat(from, formatted, to, ((DecimalFormat)nf).getMultiplier());
482 
483         /* ------------------------------------------------------------------ */
484 
485         ((DecimalFormat)nf).applyPattern("0.###E0");
486         setDigits(nf, 1, 1, 363, 0);
487 
488         // From: -1012...7890123456789
489         // To:   -1.012...789012E370
490         formatted.setLength(0);
491         from = "-1"  + fra + "0123456789";
492         to   = "-1." + fra + "012E370";
493         nf.format(new BigDecimal(from), formatted, new FieldPosition(0));
494         checkFormat(from, formatted, to, ((DecimalFormat)nf).getMultiplier());
495 
496         /* ------------------------------------------------------------------ */
497 
498         ((DecimalFormat)nf).applyPattern("#,##0.###");
499         setDigits(nf, Integer.MAX_VALUE, 1, Integer.MAX_VALUE, 720);
500 
501         // From: 1234...78900000...0000.0...0
502         // To:   1,234...7,890,000,0...0,000.0...0
503         formatted.setLength(0);
504         from = nonsep_int + nonsep_zero + "." + nonsep_zero + nonsep_zero;
505         to   = sep_int + "," + sep_zero + "." + nonsep_zero + nonsep_zero;
506         nf.format(new BigDecimal(from), formatted, new FieldPosition(0));
507         checkFormat(from, formatted, to, ((DecimalFormat)nf).getMultiplier());
508 
509         /* ------------------------------------------------------------------ */
510 
511         // From: -1234...78900000...0000.0...0
512         // To:   -1,234...7,890,000,0...0,000.0...0
513         formatted.setLength(0);
514         from = "-" + nonsep_int + nonsep_zero + "." + nonsep_zero + nonsep_zero;
515         to   = "-" + sep_int + "," + sep_zero + "." + nonsep_zero + nonsep_zero;
516         nf.format(new BigDecimal(from), formatted, new FieldPosition(0));
517         checkFormat(from, formatted, to, ((DecimalFormat)nf).getMultiplier());
518     }
519 
520     /**
521      * Test for normal big numbers which have the fraction part with multiplier
522      */
test_Format_in_NumberFormat_BigDecimal_usingMultiplier()523     void test_Format_in_NumberFormat_BigDecimal_usingMultiplier() {
524         String from, to;
525 
526         NumberFormat nf = NumberFormat.getInstance(Locale.US);
527         ((DecimalFormat)nf).applyPattern("#,##0.###");
528         setDigits(nf, Integer.MAX_VALUE, 1, Integer.MAX_VALUE, 0);
529         ((DecimalFormat)nf).setMultiplier(250000000);
530         ((DecimalFormat)nf).setDecimalSeparatorAlwaysShown(true);
531 
532         // From: 1000...0000.000...000
533         // To:   250,0...0,000.
534         formatted.setLength(0);
535         from = "1"          + nonsep_zero + "." + nonsep_zero;
536         to   = "250,000,000," + sep_zero    + ".";
537         nf.format(new BigDecimal(from), formatted, new FieldPosition(0));
538         checkFormat(from, formatted, to, ((DecimalFormat)nf).getMultiplier());
539 
540         /* ------------------------------------------------------------------ */
541 
542         ((DecimalFormat)nf).setDecimalSeparatorAlwaysShown(false);
543 
544         // From: -1000...0000.000...000
545         // To:   -250,0...0,000
546         formatted.setLength(0);
547         from = "-1"         + nonsep_zero + "." + nonsep_zero;
548         to   = "-250,000,000," + sep_zero;
549         nf.format(new BigDecimal(from), formatted, new FieldPosition(0));
550         checkFormat(from, formatted, to, ((DecimalFormat)nf).getMultiplier());
551 
552         /* ------------------------------------------------------------------ */
553 
554         ((DecimalFormat)nf).applyPattern("#,##0.###");
555         setDigits(nf, Integer.MAX_VALUE, 1, Integer.MAX_VALUE, 0);
556         ((DecimalFormat)nf).setMultiplier(-250000000);
557         ((DecimalFormat)nf).setDecimalSeparatorAlwaysShown(true);
558 
559         // From: 1000...0000.000...000
560         // To:   -250,0...0,000.
561         formatted.setLength(0);
562         from = "1"          + nonsep_zero + "." + nonsep_zero;
563         to   = "-250,000,000," + sep_zero    + ".";
564         nf.format(new BigDecimal(from), formatted, new FieldPosition(0));
565         checkFormat(from, formatted, to, ((DecimalFormat)nf).getMultiplier());
566 
567         /* ------------------------------------------------------------------ */
568 
569         ((DecimalFormat)nf).setDecimalSeparatorAlwaysShown(false);
570 
571         // From: -1000...0000.000...000
572         // To:   250,0...0,000
573         formatted.setLength(0);
574         from = "-1"         + nonsep_zero + "." + nonsep_zero;
575         to   = "250,000,000," + sep_zero;
576         nf.format(new BigDecimal(from), formatted, new FieldPosition(0));
577         checkFormat(from, formatted, to, ((DecimalFormat)nf).getMultiplier());
578     }
579 
580     /**
581      * Test for normal big numbers which don't have the fraction part
582      */
test_Format_in_NumberFormat_BigInteger()583     void test_Format_in_NumberFormat_BigInteger() {
584         String from, to;
585 
586         NumberFormat nf = NumberFormat.getInstance(Locale.US);
587         if (!(nf instanceof DecimalFormat)) {
588             throw new RuntimeException("Couldn't get DecimalFormat instance.");
589         }
590 
591         ((DecimalFormat)nf).applyPattern("#,##0.###");
592         setDigits(nf, Integer.MAX_VALUE, 1, Integer.MAX_VALUE, 0);
593 
594         // From: 1234...7890
595         // To:   123,4...7,890
596         formatted.setLength(0);
597         from = nonsep_int;
598         to   = sep_int;
599         nf.format(new BigInteger(from), formatted, new FieldPosition(0));
600         checkFormat(from, formatted, to, ((DecimalFormat)nf).getMultiplier());
601 
602         /* ------------------------------------------------------------------ */
603 
604         // From: -1234...7890
605         // To:   -123,4...7,890
606         //      ~~~~~~~~~~~~~ : FieldPosition(INTEGER_FIELD)
607         fp = new FieldPosition(DecimalFormat.INTEGER_FIELD);
608         formatted.setLength(0);
609         from = "-" + nonsep_int;
610         to   = "-" + sep_int;
611         nf.format(new BigInteger(from), formatted, fp);
612         checkFormat(from, formatted, to, ((DecimalFormat)nf).getMultiplier());
613         checkFieldPosition(from, fp, 1, 480);
614 
615         /* ------------------------------------------------------------------ */
616 
617         // From: 000...0001234...7890
618         // To:   123,4...7,890
619         formatted.setLength(0);
620         from = nonsep_zero + nonsep_int;
621         to   = sep_int;
622         nf.format(new BigInteger(from), formatted, new FieldPosition(0));
623         checkFormat(from, formatted, to, ((DecimalFormat)nf).getMultiplier());
624 
625         /* ------------------------------------------------------------------ */
626 
627         // From: -000...0001234...7890
628         // To:   -123,4...7,890
629         //       ~ : FieldPosition(SIGN)
630         fp = new FieldPosition(NumberFormat.Field.SIGN);
631         formatted.setLength(0);
632         from = "-" + nonsep_zero + nonsep_int;
633         to   = "-" + sep_int;
634         nf.format(new BigInteger(from), formatted, fp);
635         checkFormat(from, formatted, to, ((DecimalFormat)nf).getMultiplier());
636         checkFieldPosition(from, fp, 0, 1);
637 
638         /* ------------------------------------------------------------------ */
639 
640         // From: 000...0000
641         // To:   0
642         formatted.setLength(0);
643         from = nonsep_zero;
644         to   = "0";
645         nf.format(new BigInteger(from), formatted, new FieldPosition(0));
646         checkFormat(from, formatted, to, ((DecimalFormat)nf).getMultiplier());
647 
648         /* ------------------------------------------------------------------ */
649 
650         ((DecimalFormat)nf).applyPattern("#,##0.0");
651         setDigits(nf, Integer.MAX_VALUE, 1, Integer.MAX_VALUE, 1);
652 
653         // From: -000...0000
654         // To:   0.0
655         fp = new FieldPosition(NumberFormat.Field.DECIMAL_SEPARATOR);
656         formatted.setLength(0);
657         from = "-" + nonsep_zero;
658         to   = "0.0";
659         nf.format(new BigInteger(from), formatted, fp);
660         checkFormat(from, formatted, to, ((DecimalFormat)nf).getMultiplier());
661         checkFieldPosition(from, fp, 1, 2);
662 
663         /* ------------------------------------------------------------------ */
664 
665         ((DecimalFormat)nf).applyPattern("0.###E0");
666         setDigits(nf, 1, 1, Integer.MAX_VALUE, 0);
667 
668         // From: 10123...789
669         // To  : 1.0123...789E360
670         //                  ~~~ : FieldPosition(EXPONENT)
671         fp = new FieldPosition(NumberFormat.Field.EXPONENT);
672         formatted.setLength(0);
673         from = "1"  + fra;
674         to   = "1." + fra + "E360";
675         nf.format(new BigInteger(from), formatted, fp);
676         checkFormat(from, formatted, to, ((DecimalFormat)nf).getMultiplier());
677         checkFieldPosition(from, fp, 363, 366);
678 
679         /* ------------------------------------------------------------------ */
680 
681         // From: -1012...789
682         // To  : -1.012...789E360
683         formatted.setLength(0);
684         from = "-1"  + fra;
685         to   = "-1." + fra + "E360";
686         nf.format(new BigInteger(from), formatted, new FieldPosition(0));
687         checkFormat(from, formatted, to, ((DecimalFormat)nf).getMultiplier());
688 
689         /* ------------------------------------------------------------------ */
690 
691         ((DecimalFormat)nf).applyPattern("00000.###E0");
692         setDigits(nf, 5, 5, Integer.MAX_VALUE, 720);
693 
694         // From: 12345012...789000...000
695         // To  : 12345.012...789000...000E720
696         //                              ~~~ : FieldPosition(EXPONENT)
697         fp = new FieldPosition(NumberFormat.Field.EXPONENT);
698         formatted.setLength(0);
699         from = "12345"  + fra + nonsep_zero;
700         to   = "12345." + fra + nonsep_zero + "E720";
701         nf.format(new BigInteger(from), formatted, fp);
702         checkFormat(from, formatted, to, ((DecimalFormat)nf).getMultiplier());
703         checkFieldPosition(from, fp, 727, 730);
704 
705         /* ------------------------------------------------------------------ */
706 
707         ((DecimalFormat)nf).applyPattern("00000.###E0");
708         setDigits(nf, 5, 5, Integer.MAX_VALUE, 365);
709 
710         // From: -1234567890012...789000...000
711         // To  : -12345.67890012...789E365
712         formatted.setLength(0);
713         from = "-1234567890"  + fra;
714         to   = "-12345.67890" + fra + "E365";
715         nf.format(new BigInteger(from), formatted, new FieldPosition(0));
716         checkFormat(from, formatted, to, ((DecimalFormat)nf).getMultiplier());
717     }
718 
719     /**
720      * Test for normal big numbers which don't have the fraction part with
721      * multiplier
722      */
test_Format_in_NumberFormat_BigInteger_usingMultiplier()723     void test_Format_in_NumberFormat_BigInteger_usingMultiplier() {
724         String from, to;
725 
726         NumberFormat nf = NumberFormat.getInstance(Locale.US);
727 
728         ((DecimalFormat)nf).applyPattern("#,##0.###");
729         ((DecimalFormat)nf).setMultiplier(250000000);
730         setDigits(nf, Integer.MAX_VALUE, 1, Integer.MAX_VALUE, 0);
731 
732         // From: 1000...0000
733         // To:   250,0...0,000
734         formatted.setLength(0);
735         from = "1" + nonsep_zero;
736         to   = "250,000,000," + sep_zero;
737         nf.format(new BigInteger(from), formatted, new FieldPosition(0));
738         checkFormat(from, formatted, to, ((DecimalFormat)nf).getMultiplier());
739 
740         /* ------------------------------------------------------------------ */
741 
742         // From: -1000...0000
743         // To:   -250,0...0,000
744         formatted.setLength(0);
745         from = "-1" + nonsep_zero;
746         to   = "-250,000,000," + sep_zero;
747         nf.format(new BigInteger(from), formatted, new FieldPosition(0));
748         checkFormat(from, formatted, to, ((DecimalFormat)nf).getMultiplier());
749         /* ------------------------------------------------------------------ */
750 
751         ((DecimalFormat)nf).applyPattern("#,##0.###");
752         ((DecimalFormat)nf).setMultiplier(-250000000);
753         setDigits(nf, Integer.MAX_VALUE, 1, Integer.MAX_VALUE, 0);
754 
755         // From: 1000...0000
756         // To:   -250,0...0,000
757         formatted.setLength(0);
758         from = "1" + nonsep_zero;
759         to   = "-250,000,000," + sep_zero;
760         nf.format(new BigInteger(from), formatted, new FieldPosition(0));
761         checkFormat(from, formatted, to, ((DecimalFormat)nf).getMultiplier());
762 
763         /* ------------------------------------------------------------------ */
764 
765         // From: -1000...0000
766         // To:   250,0...0,000
767         formatted.setLength(0);
768         from = "-1" + nonsep_zero;
769         to   = "250,000,000," + sep_zero;
770         nf.format(new BigInteger(from), formatted, new FieldPosition(0));
771         checkFormat(from, formatted, to, ((DecimalFormat)nf).getMultiplier());
772     }
773 
774     /**
775      * Test for normal Long numbers when maximum and minimum digits are
776      * specified
777      */
test_Format_in_NumberFormat_Long_checkDigits()778     void test_Format_in_NumberFormat_Long_checkDigits() {
779         String from, to;
780 
781         NumberFormat nf = NumberFormat.getInstance(Locale.US);
782         if (!(nf instanceof DecimalFormat)) {
783             throw new RuntimeException("Couldn't get DecimalFormat instance.");
784         }
785 
786         ((DecimalFormat)nf).applyPattern("#,##0.###");
787         setDigits(nf, Integer.MAX_VALUE, 360, Integer.MAX_VALUE, 0);
788 
789         // From: 1234567890
790         // To:   000,0...0,000,123,456,789
791         //       -------------
792         //       300 zeros
793         formatted.setLength(0);
794         from = "123456789";
795         to   = sep_zero.substring(0, 399) + ",123,456,789";
796         nf.format(123456789L, formatted, new FieldPosition(0));
797         checkFormat(from, formatted, to, ((DecimalFormat)nf).getMultiplier());
798 
799         /* ------------------------------------------------------------------ */
800 
801         ((DecimalFormat)nf).applyPattern("##0.###");
802         ((DecimalFormat)nf).setMultiplier(-1);
803         setDigits(nf, Integer.MAX_VALUE, 360, Integer.MAX_VALUE, 360);
804 
805         // From: 1234567890
806         // To:   -0000...0000123456789.000...000
807         //      -------------
808         //        300 zeros
809         formatted.setLength(0);
810         from = "123456789";
811         to   = "-" + nonsep_zero.substring(0, 300) + "123456789." +
812                nonsep_zero.substring(0, 340);
813         nf.format(123456789L, formatted, new FieldPosition(0));
814         checkFormat(from, formatted, to, ((DecimalFormat)nf).getMultiplier());
815 
816         /* ------------------------------------------------------------------ */
817 
818         ((DecimalFormat)nf).applyPattern("#,##0.###");
819         ((DecimalFormat)nf).setMultiplier(Integer.MAX_VALUE);
820         setDigits(nf, Integer.MAX_VALUE, 360, Integer.MAX_VALUE, 0);
821 
822         // From: Long.MAX_VALUE
823         // To:   000,0...0,000,019,807,040,619,342,712,359,383,728,129
824         //       ---------------
825         //       280 zeros
826         formatted.setLength(0);
827         from = Long.toString(Long.MAX_VALUE);
828         to   = sep_zero.substring(0, 373) +
829                "19,807,040,619,342,712,359,383,728,129";
830         nf.format(Long.MAX_VALUE, formatted, new FieldPosition(0));
831         checkFormat(from, formatted, to, ((DecimalFormat)nf).getMultiplier());
832 
833         /* ------------------------------------------------------------------ */
834 
835         ((DecimalFormat)nf).applyPattern("0.###E0");
836         ((DecimalFormat)nf).setMultiplier(Integer.MIN_VALUE);
837         setDigits(nf, 1, 1, Integer.MAX_VALUE, 360);
838 
839         // From: Long.MAX_VALUE
840         // To:   -1.9807040628566084396238503936000...000E28
841         //                                      ---------
842         //                                      312 zeros
843         formatted.setLength(0);
844         from = Long.toString(Long.MAX_VALUE);
845         to   = "-1.9807040628566084396238503936" +
846                nonsep_zero.substring(0, 312) + "E28";
847         nf.format(Long.MAX_VALUE, formatted, new FieldPosition(0));
848         checkFormat(from, formatted, to, ((DecimalFormat)nf).getMultiplier());
849 
850         /* ------------------------------------------------------------------ */
851 
852         ((DecimalFormat)nf).applyPattern("##0.###E0");
853         ((DecimalFormat)nf).setMultiplier(Integer.MAX_VALUE);
854         setDigits(nf, Integer.MAX_VALUE, 360, Integer.MAX_VALUE, 360);
855 
856         // From: Long.MIN_VALUE
857         // To:   -198070406193427123615312117760000...0000.000...000E-280
858         //                                     ----------- ---------
859         //                                      280 zeros  340 zeros
860         formatted.setLength(0);
861         from = Long.toString(Long.MIN_VALUE);
862         to   = "-19807040619342712361531211776" +
863                nonsep_zero.substring(0, 280) + "." +
864                nonsep_zero.substring(0, 340) + "E-280";
865         nf.format(Long.MIN_VALUE, formatted, new FieldPosition(0));
866         checkFormat(from, formatted, to, ((DecimalFormat)nf).getMultiplier());
867 
868         /* ------------------------------------------------------------------ */
869 
870         ((DecimalFormat)nf).applyPattern("#,##0.###");
871         ((DecimalFormat)nf).setMultiplier(Integer.MIN_VALUE);
872         setDigits(nf, Integer.MAX_VALUE, 360, Integer.MAX_VALUE, 360);
873 
874         // From: Long.MIN_VALUE
875         // To:   000,0...0,000,019,807,040,628,566,084,398,385,987,584.000...000
876         //       ---------------                                       ---------
877         //          280 zeros                                          340 zeros
878         formatted.setLength(0);
879         from = Long.toString(Long.MIN_VALUE);
880         to   = sep_zero.substring(0, 373) +
881                "19,807,040,628,566,084,398,385,987,584." +
882                nonsep_zero.substring(0, 340);
883         nf.format(Long.MIN_VALUE, formatted, new FieldPosition(0));
884         checkFormat(from, formatted, to, ((DecimalFormat)nf).getMultiplier());
885     }
886 
887     /**
888      * Test for special numbers
889      *    Double.NaN
890      *    Double.POSITIVE_INFINITY
891      *    Double.NEGATIVE_INFINITY
892      */
test_Format_in_NumberFormat_SpecialNumber()893     void test_Format_in_NumberFormat_SpecialNumber() {
894         String from, to;
895 
896         NumberFormat nf = NumberFormat.getInstance(Locale.US);
897         if (!(nf instanceof DecimalFormat)) {
898             throw new RuntimeException("Couldn't get DecimalFormat instance.");
899         }
900 
901         ((DecimalFormat)nf).applyPattern("#,##0.###");
902         setDigits(nf, Integer.MAX_VALUE, 1, Integer.MAX_VALUE, 0);
903 
904         double[] numbers = {
905             -0.0, 0.0, Double.NaN,
906             Double.POSITIVE_INFINITY, 5.1, 5.0,
907             Double.NEGATIVE_INFINITY, -5.1, -5.0,
908         };
909         int multipliers[] = {0, 5, -5};
910         String[][] expected = {
911             {"-0", "0", "\ufffd", "\ufffd", "0", "0", "\ufffd", "-0", "-0"},
912             {"-0", "0", "\ufffd", "\u221e", "25.5", "25", "-\u221e", "-25.5",
913              "-25"},
914             {"0", "-0", "\ufffd", "-\u221e", "-25.5", "-25", "\u221e", "25.5",
915              "25"},
916         };
917 
918         for (int i = 0; i < multipliers.length; i++) {
919             ((DecimalFormat)nf).setMultiplier(multipliers[i]);
920             for (int j = 0; j < numbers.length; j++) {
921                 formatted.setLength(0);
922                 from = String.valueOf(numbers[j]);
923                 nf.format(numbers[j], formatted, new FieldPosition(0));
924                 checkFormat(from, formatted, expected[i][j],
925                             ((DecimalFormat)nf).getMultiplier());
926             }
927         }
928     }
929 
930     /**
931      * Test for Long.MIN_VALUE
932      *   (Formatting Long.MIN_VALUE w/ multiplier=-1 used to return a wrong
933      *    number.)
934      */
test_Format_in_NumberFormat_Other()935     void test_Format_in_NumberFormat_Other() {
936         String from, to;
937 
938         NumberFormat nf = NumberFormat.getInstance(Locale.US);
939         if (!(nf instanceof DecimalFormat)) {
940             throw new RuntimeException("Couldn't get DecimalFormat instance.");
941         }
942 
943         long[] numbers = {
944             Long.MIN_VALUE,
945         };
946         int multipliers[] = {1, -1};
947         String[][] expected = {
948             {"-9,223,372,036,854,775,808"},     // Long.MIN_VALUE
949             {"9,223,372,036,854,775,808"},      // Long.MIN_VALUE * (-1)
950         };
951 
952         for (int i = 0; i < multipliers.length; i++) {
953             ((DecimalFormat)nf).setMultiplier(multipliers[i]);
954             for (int j = 0; j < numbers.length; j++) {
955                 formatted.setLength(0);
956                 from = String.valueOf(numbers[j]);
957                 nf.format(numbers[j], formatted, new FieldPosition(0));
958                 checkFormat(from, formatted, expected[i][j],
959                             ((DecimalFormat)nf).getMultiplier());
960             }
961         }
962     }
963 
964     /**
965      * Test for MessageFormat
966      */
test_Format_in_MessageFormat()967     void test_Format_in_MessageFormat() {
968         MessageFormat mf = new MessageFormat(
969             "  {0, number}\n" +
970             "  {0, number, integer}\n" +
971             "  {0, number, currency}\n" +
972             "  {0, number, percent}\n" +
973             "  {0, number,0.###########E0}\n" +
974 
975             "  {1, number}\n" +
976             "  {1, number, integer}\n" +
977             "  {1, number, currency}\n" +
978             "  {1, number, percent}\n" +
979             "  {1, number,0.#######E0}\n",
980             Locale.US
981         );
982         Object[] testArgs = {
983             new BigInteger("9876543210987654321098765432109876543210"),
984             new BigDecimal("-12345678901234567890.98765432109876543210987654321"),
985         };
986         String expected =
987             "  9,876,543,210,987,654,321,098,765,432,109,876,543,210\n" +
988             "  9,876,543,210,987,654,321,098,765,432,109,876,543,210\n" +
989             "  $9,876,543,210,987,654,321,098,765,432,109,876,543,210.00\n" +
990             "  987,654,321,098,765,432,109,876,543,210,987,654,321,000%\n" +
991             "  9.87654321099E39\n" +
992 
993             "  -12,345,678,901,234,567,890.988\n" +
994             "  -12,345,678,901,234,567,891\n" +
995             "  ($12,345,678,901,234,567,890.99)\n" +
996             "  -1,234,567,890,123,456,789,099%\n" +
997             "  -1.2345679E19\n"
998         ;
999 
1000         if (!expected.equals(mf.format(testArgs))) {
1001             errln("Wrong format.\n      got:\n" + mf.format(testArgs) +
1002                   "     expected:\n" + expected);
1003         }
1004     }
1005 
setDigits(NumberFormat nf, int i_max, int i_min, int f_max, int f_min)1006     private void setDigits(NumberFormat nf,
1007                            int i_max, int i_min, int f_max, int f_min) {
1008         nf.setMaximumIntegerDigits(i_max);
1009         nf.setMinimumIntegerDigits(i_min);
1010         nf.setMaximumFractionDigits(f_max);
1011         nf.setMinimumFractionDigits(f_min);
1012     }
1013 
checkFormat(String orig, StringBuffer got, String expected, int multiplier)1014     private void checkFormat(String orig, StringBuffer got, String expected,
1015                              int multiplier) {
1016         if (!expected.equals(new String(got))) {
1017             errln("Formatting... failed." +
1018                   "\n   original:   " + orig +
1019                   "\n   multiplier: " + multiplier +
1020                   "\n   formatted:  " + got +
1021                   "\n   expected:   " + expected + "\n");
1022         }
1023     }
1024 
checkFieldPosition(String orig, FieldPosition fp, int begin, int end)1025     private void checkFieldPosition(String orig, FieldPosition fp, int begin,
1026                                     int end) {
1027         int position;
1028 
1029         if ((position = fp.getBeginIndex()) != begin) {
1030             errln("Formatting... wrong Begin index returned for " +
1031                   fp.getFieldAttribute() + "." +
1032                   "\n   original: " + orig +
1033                   "\n   got:      " + position +
1034                   "\n   expected: " + begin + "\n");
1035         }
1036         if ((position = fp.getEndIndex()) != end) {
1037             errln("Formatting... wrong End index returned for " +
1038                   fp.getFieldAttribute() + "." +
1039                   "\n   original: " + orig +
1040                   "\n   got:      " + position +
1041                   "\n   expected: " + end + "\n");
1042         }
1043     }
1044 }
1045