< prev index next >

test/jdk/java/text/Format/NumberFormat/BigDecimalFormat.java

Print this page

   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 
  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," +

  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      */

 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);

 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      */

 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);

 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      */

 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         /* ------------------------------------------------------------------ */

 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      */

 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

 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      */

 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());

 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      */

 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",

 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      */

 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      */

 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 
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 
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 
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 }

   1 /*
   2  * Copyright (c) 2003, 2024, 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  * @run junit/othervm -Djava.locale.providers=COMPAT,SPI BigDecimalFormat

  29  */
  30 
  31 import java.math.BigDecimal;
  32 import java.math.BigInteger;
  33 import java.text.DecimalFormat;
  34 import java.text.FieldPosition;
  35 import java.text.MessageFormat;
  36 import java.text.NumberFormat;
  37 import java.util.Locale;
  38 
  39 import org.junit.jupiter.api.Test;
  40 
  41 import static org.junit.jupiter.api.Assertions.fail;
  42 
  43 public class BigDecimalFormat {
  44 
  45     static final String nonsep_int =
  46         "123456789012345678901234567890123456789012345678901234567890" +
  47         "123456789012345678901234567890123456789012345678901234567890" +
  48         "123456789012345678901234567890123456789012345678901234567890" +
  49         "123456789012345678901234567890123456789012345678901234567890" +
  50         "123456789012345678901234567890123456789012345678901234567890" +
  51         "123456789012345678901234567890123456789012345678901234567890";
  52 
  53     static final String sep_int =
  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," +

  84         "000,000,000,000,000,000,000,000,000,000," +
  85         "000,000,000,000,000,000,000,000,000,000," +
  86         "000,000,000,000,000,000,000,000,000,000," +
  87         "000,000,000,000,000,000,000,000,000,000";
  88 
  89     static final String fra =
  90         "012345678901234567890123456789012345678901234567890123456789" +
  91         "012345678901234567890123456789012345678901234567890123456789" +
  92         "012345678901234567890123456789012345678901234567890123456789" +
  93         "012345678901234567890123456789012345678901234567890123456789" +
  94         "012345678901234567890123456789012345678901234567890123456789" +
  95         "012345678901234567890123456789012345678901234567890123456789";
  96 
  97 
  98     StringBuffer formatted = new StringBuffer(1000);
  99     FieldPosition fp;
 100 
 101     /**
 102      * Test for normal big numbers which have the fraction part
 103      */
 104     @Test
 105     void test_Format_in_NumberFormat_BigDecimal() {
 106         String from, to;
 107 
 108         NumberFormat nf = NumberFormat.getInstance(Locale.US);
 109         ((DecimalFormat)nf).applyPattern("#,##0.###");
 110         setDigits(nf, Integer.MAX_VALUE, 1, Integer.MAX_VALUE, 0);
 111 
 112         // From: 0.000...789
 113         // To:   0.000...789 (same as From)
 114         formatted.setLength(0);
 115         from = "0." + nonsep_zero + "123456789";
 116         nf.format(new BigDecimal(from), formatted, new FieldPosition(0));
 117         checkFormat(from, formatted, from, ((DecimalFormat)nf).getMultiplier());
 118 
 119         /* ------------------------------------------------------------------ */
 120 
 121         // From: -0.000...789
 122         // To:   -0.000...789 (same as From)
 123         //       ~ : FieldPosition(SIGN)
 124         fp = new FieldPosition(NumberFormat.Field.SIGN);

 506         formatted.setLength(0);
 507         from = nonsep_int + nonsep_zero + "." + nonsep_zero + nonsep_zero;
 508         to   = sep_int + "," + sep_zero + "." + nonsep_zero + nonsep_zero;
 509         nf.format(new BigDecimal(from), formatted, new FieldPosition(0));
 510         checkFormat(from, formatted, to, ((DecimalFormat)nf).getMultiplier());
 511 
 512         /* ------------------------------------------------------------------ */
 513 
 514         // From: -1234...78900000...0000.0...0
 515         // To:   -1,234...7,890,000,0...0,000.0...0
 516         formatted.setLength(0);
 517         from = "-" + nonsep_int + nonsep_zero + "." + nonsep_zero + nonsep_zero;
 518         to   = "-" + sep_int + "," + sep_zero + "." + nonsep_zero + nonsep_zero;
 519         nf.format(new BigDecimal(from), formatted, new FieldPosition(0));
 520         checkFormat(from, formatted, to, ((DecimalFormat)nf).getMultiplier());
 521     }
 522 
 523     /**
 524      * Test for normal big numbers which have the fraction part with multiplier
 525      */
 526     @Test
 527     void test_Format_in_NumberFormat_BigDecimal_usingMultiplier() {
 528         String from, to;
 529 
 530         NumberFormat nf = NumberFormat.getInstance(Locale.US);
 531         ((DecimalFormat)nf).applyPattern("#,##0.###");
 532         setDigits(nf, Integer.MAX_VALUE, 1, Integer.MAX_VALUE, 0);
 533         ((DecimalFormat)nf).setMultiplier(250000000);
 534         ((DecimalFormat)nf).setDecimalSeparatorAlwaysShown(true);
 535 
 536         // From: 1000...0000.000...000
 537         // To:   250,0...0,000.
 538         formatted.setLength(0);
 539         from = "1"          + nonsep_zero + "." + nonsep_zero;
 540         to   = "250,000,000," + sep_zero    + ".";
 541         nf.format(new BigDecimal(from), formatted, new FieldPosition(0));
 542         checkFormat(from, formatted, to, ((DecimalFormat)nf).getMultiplier());
 543 
 544         /* ------------------------------------------------------------------ */
 545 
 546         ((DecimalFormat)nf).setDecimalSeparatorAlwaysShown(false);

 567         to   = "-250,000,000," + sep_zero    + ".";
 568         nf.format(new BigDecimal(from), formatted, new FieldPosition(0));
 569         checkFormat(from, formatted, to, ((DecimalFormat)nf).getMultiplier());
 570 
 571         /* ------------------------------------------------------------------ */
 572 
 573         ((DecimalFormat)nf).setDecimalSeparatorAlwaysShown(false);
 574 
 575         // From: -1000...0000.000...000
 576         // To:   250,0...0,000
 577         formatted.setLength(0);
 578         from = "-1"         + nonsep_zero + "." + nonsep_zero;
 579         to   = "250,000,000," + sep_zero;
 580         nf.format(new BigDecimal(from), formatted, new FieldPosition(0));
 581         checkFormat(from, formatted, to, ((DecimalFormat)nf).getMultiplier());
 582     }
 583 
 584     /**
 585      * Test for normal big numbers which don't have the fraction part
 586      */
 587     @Test
 588     void test_Format_in_NumberFormat_BigInteger() {
 589         String from, to;
 590 
 591         NumberFormat nf = NumberFormat.getInstance(Locale.US);
 592         if (!(nf instanceof DecimalFormat)) {
 593             throw new RuntimeException("Couldn't get DecimalFormat instance.");
 594         }
 595 
 596         ((DecimalFormat)nf).applyPattern("#,##0.###");
 597         setDigits(nf, Integer.MAX_VALUE, 1, Integer.MAX_VALUE, 0);
 598 
 599         // From: 1234...7890
 600         // To:   123,4...7,890
 601         formatted.setLength(0);
 602         from = nonsep_int;
 603         to   = sep_int;
 604         nf.format(new BigInteger(from), formatted, new FieldPosition(0));
 605         checkFormat(from, formatted, to, ((DecimalFormat)nf).getMultiplier());
 606 
 607         /* ------------------------------------------------------------------ */

 708         checkFieldPosition(from, fp, 727, 730);
 709 
 710         /* ------------------------------------------------------------------ */
 711 
 712         ((DecimalFormat)nf).applyPattern("00000.###E0");
 713         setDigits(nf, 5, 5, Integer.MAX_VALUE, 365);
 714 
 715         // From: -1234567890012...789000...000
 716         // To  : -12345.67890012...789E365
 717         formatted.setLength(0);
 718         from = "-1234567890"  + fra;
 719         to   = "-12345.67890" + fra + "E365";
 720         nf.format(new BigInteger(from), formatted, new FieldPosition(0));
 721         checkFormat(from, formatted, to, ((DecimalFormat)nf).getMultiplier());
 722     }
 723 
 724     /**
 725      * Test for normal big numbers which don't have the fraction part with
 726      * multiplier
 727      */
 728     @Test
 729     void test_Format_in_NumberFormat_BigInteger_usingMultiplier() {
 730         String from, to;
 731 
 732         NumberFormat nf = NumberFormat.getInstance(Locale.US);
 733 
 734         ((DecimalFormat)nf).applyPattern("#,##0.###");
 735         ((DecimalFormat)nf).setMultiplier(250000000);
 736         setDigits(nf, Integer.MAX_VALUE, 1, Integer.MAX_VALUE, 0);
 737 
 738         // From: 1000...0000
 739         // To:   250,0...0,000
 740         formatted.setLength(0);
 741         from = "1" + nonsep_zero;
 742         to   = "250,000,000," + sep_zero;
 743         nf.format(new BigInteger(from), formatted, new FieldPosition(0));
 744         checkFormat(from, formatted, to, ((DecimalFormat)nf).getMultiplier());
 745 
 746         /* ------------------------------------------------------------------ */
 747 
 748         // From: -1000...0000

 764         from = "1" + nonsep_zero;
 765         to   = "-250,000,000," + sep_zero;
 766         nf.format(new BigInteger(from), formatted, new FieldPosition(0));
 767         checkFormat(from, formatted, to, ((DecimalFormat)nf).getMultiplier());
 768 
 769         /* ------------------------------------------------------------------ */
 770 
 771         // From: -1000...0000
 772         // To:   250,0...0,000
 773         formatted.setLength(0);
 774         from = "-1" + nonsep_zero;
 775         to   = "250,000,000," + sep_zero;
 776         nf.format(new BigInteger(from), formatted, new FieldPosition(0));
 777         checkFormat(from, formatted, to, ((DecimalFormat)nf).getMultiplier());
 778     }
 779 
 780     /**
 781      * Test for normal Long numbers when maximum and minimum digits are
 782      * specified
 783      */
 784     @Test
 785     void test_Format_in_NumberFormat_Long_checkDigits() {
 786         String from, to;
 787 
 788         NumberFormat nf = NumberFormat.getInstance(Locale.US);
 789         if (!(nf instanceof DecimalFormat)) {
 790             throw new RuntimeException("Couldn't get DecimalFormat instance.");
 791         }
 792 
 793         ((DecimalFormat)nf).applyPattern("#,##0.###");
 794         setDigits(nf, Integer.MAX_VALUE, 360, Integer.MAX_VALUE, 0);
 795 
 796         // From: 1234567890
 797         // To:   000,0...0,000,123,456,789
 798         //       -------------
 799         //       300 zeros
 800         formatted.setLength(0);
 801         from = "123456789";
 802         to   = sep_zero.substring(0, 399) + ",123,456,789";
 803         nf.format(123456789L, formatted, new FieldPosition(0));
 804         checkFormat(from, formatted, to, ((DecimalFormat)nf).getMultiplier());

 880 
 881         // From: Long.MIN_VALUE
 882         // To:   000,0...0,000,019,807,040,628,566,084,398,385,987,584.000...000
 883         //       ---------------                                       ---------
 884         //          280 zeros                                          340 zeros
 885         formatted.setLength(0);
 886         from = Long.toString(Long.MIN_VALUE);
 887         to   = sep_zero.substring(0, 373) +
 888                "19,807,040,628,566,084,398,385,987,584." +
 889                nonsep_zero.substring(0, 340);
 890         nf.format(Long.MIN_VALUE, formatted, new FieldPosition(0));
 891         checkFormat(from, formatted, to, ((DecimalFormat)nf).getMultiplier());
 892     }
 893 
 894     /**
 895      * Test for special numbers
 896      *    Double.NaN
 897      *    Double.POSITIVE_INFINITY
 898      *    Double.NEGATIVE_INFINITY
 899      */
 900     @Test
 901     void test_Format_in_NumberFormat_SpecialNumber() {
 902         String from, to;
 903 
 904         NumberFormat nf = NumberFormat.getInstance(Locale.US);
 905         if (!(nf instanceof DecimalFormat)) {
 906             throw new RuntimeException("Couldn't get DecimalFormat instance.");
 907         }
 908 
 909         ((DecimalFormat)nf).applyPattern("#,##0.###");
 910         setDigits(nf, Integer.MAX_VALUE, 1, Integer.MAX_VALUE, 0);
 911 
 912         double[] numbers = {
 913             -0.0, 0.0, Double.NaN,
 914             Double.POSITIVE_INFINITY, 5.1, 5.0,
 915             Double.NEGATIVE_INFINITY, -5.1, -5.0,
 916         };
 917         int multipliers[] = {0, 5, -5};
 918         String[][] expected = {
 919             {"-0", "0", "\ufffd", "\ufffd", "0", "0", "\ufffd", "-0", "-0"},
 920             {"-0", "0", "\ufffd", "\u221e", "25.5", "25", "-\u221e", "-25.5",

 923              "25"},
 924         };
 925 
 926         for (int i = 0; i < multipliers.length; i++) {
 927             ((DecimalFormat)nf).setMultiplier(multipliers[i]);
 928             for (int j = 0; j < numbers.length; j++) {
 929                 formatted.setLength(0);
 930                 from = String.valueOf(numbers[j]);
 931                 nf.format(numbers[j], formatted, new FieldPosition(0));
 932                 checkFormat(from, formatted, expected[i][j],
 933                             ((DecimalFormat)nf).getMultiplier());
 934             }
 935         }
 936     }
 937 
 938     /**
 939      * Test for Long.MIN_VALUE
 940      *   (Formatting Long.MIN_VALUE w/ multiplier=-1 used to return a wrong
 941      *    number.)
 942      */
 943     @Test
 944     void test_Format_in_NumberFormat_Other() {
 945         String from, to;
 946 
 947         NumberFormat nf = NumberFormat.getInstance(Locale.US);
 948         if (!(nf instanceof DecimalFormat)) {
 949             throw new RuntimeException("Couldn't get DecimalFormat instance.");
 950         }
 951 
 952         long[] numbers = {
 953             Long.MIN_VALUE,
 954         };
 955         int multipliers[] = {1, -1};
 956         String[][] expected = {
 957             {"-9,223,372,036,854,775,808"},     // Long.MIN_VALUE
 958             {"9,223,372,036,854,775,808"},      // Long.MIN_VALUE * (-1)
 959         };
 960 
 961         for (int i = 0; i < multipliers.length; i++) {
 962             ((DecimalFormat)nf).setMultiplier(multipliers[i]);
 963             for (int j = 0; j < numbers.length; j++) {
 964                 formatted.setLength(0);
 965                 from = String.valueOf(numbers[j]);
 966                 nf.format(numbers[j], formatted, new FieldPosition(0));
 967                 checkFormat(from, formatted, expected[i][j],
 968                             ((DecimalFormat)nf).getMultiplier());
 969             }
 970         }
 971     }
 972 
 973     /**
 974      * Test for MessageFormat
 975      */
 976     @Test
 977     void test_Format_in_MessageFormat() {
 978         MessageFormat mf = new MessageFormat(
 979             "  {0, number}\n" +
 980             "  {0, number, integer}\n" +
 981             "  {0, number, currency}\n" +
 982             "  {0, number, percent}\n" +
 983             "  {0, number,0.###########E0}\n" +
 984 
 985             "  {1, number}\n" +
 986             "  {1, number, integer}\n" +
 987             "  {1, number, currency}\n" +
 988             "  {1, number, percent}\n" +
 989             "  {1, number,0.#######E0}\n",
 990             Locale.US
 991         );
 992         Object[] testArgs = {
 993             new BigInteger("9876543210987654321098765432109876543210"),
 994             new BigDecimal("-12345678901234567890.98765432109876543210987654321"),
 995         };
 996         String expected =
 997             "  9,876,543,210,987,654,321,098,765,432,109,876,543,210\n" +
 998             "  9,876,543,210,987,654,321,098,765,432,109,876,543,210\n" +
 999             "  $9,876,543,210,987,654,321,098,765,432,109,876,543,210.00\n" +
1000             "  987,654,321,098,765,432,109,876,543,210,987,654,321,000%\n" +
1001             "  9.87654321099E39\n" +
1002 
1003             "  -12,345,678,901,234,567,890.988\n" +
1004             "  -12,345,678,901,234,567,891\n" +
1005             "  ($12,345,678,901,234,567,890.99)\n" +
1006             "  -1,234,567,890,123,456,789,099%\n" +
1007             "  -1.2345679E19\n"
1008         ;
1009 
1010         if (!expected.equals(mf.format(testArgs))) {
1011             fail("Wrong format.\n      got:\n" + mf.format(testArgs) +
1012                   "     expected:\n" + expected);
1013         }
1014     }
1015 
1016     private void setDigits(NumberFormat nf,
1017                            int i_max, int i_min, int f_max, int f_min) {
1018         nf.setMaximumIntegerDigits(i_max);
1019         nf.setMinimumIntegerDigits(i_min);
1020         nf.setMaximumFractionDigits(f_max);
1021         nf.setMinimumFractionDigits(f_min);
1022     }
1023 
1024     private void checkFormat(String orig, StringBuffer got, String expected,
1025                              int multiplier) {
1026         if (!expected.equals(new String(got))) {
1027             fail("Formatting... failed." +
1028                   "\n   original:   " + orig +
1029                   "\n   multiplier: " + multiplier +
1030                   "\n   formatted:  " + got +
1031                   "\n   expected:   " + expected + "\n");
1032         }
1033     }
1034 
1035     private void checkFieldPosition(String orig, FieldPosition fp, int begin,
1036                                     int end) {
1037         int position;
1038 
1039         if ((position = fp.getBeginIndex()) != begin) {
1040             fail("Formatting... wrong Begin index returned for " +
1041                   fp.getFieldAttribute() + "." +
1042                   "\n   original: " + orig +
1043                   "\n   got:      " + position +
1044                   "\n   expected: " + begin + "\n");
1045         }
1046         if ((position = fp.getEndIndex()) != end) {
1047             fail("Formatting... wrong End index returned for " +
1048                   fp.getFieldAttribute() + "." +
1049                   "\n   original: " + orig +
1050                   "\n   got:      " + position +
1051                   "\n   expected: " + end + "\n");
1052         }
1053     }
1054 }
< prev index next >