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  * @summary Confirm that the decimal separator is shown when explicitly requested.
  27  * @bug 4208135
  28  */
  29 
  30 import java.math.*;
  31 import java.text.*;
  32 import java.util.*;
  33 
  34 public class Bug4208135 {
  35 
  36     static DecimalFormat df;
  37 
  38     static boolean err = false;
  39 
  40     static public void main(String[] args){
  41 
  42         Locale defaultLoc = Locale.getDefault();
  43         Locale.setDefault(Locale.US);
  44 
  45         df = new DecimalFormat();
  46 
  47         df.applyPattern("0.#E0");
  48 
  49         df.setDecimalSeparatorAlwaysShown(true);
  50         checkFormat(new Double(0.0), "0.E0");
  51         checkFormat(new Double(10.0), "1.E1");
  52         checkFormat(new Double(1000.0), "1.E3");
  53         checkFormat(new Long(0), "0.E0");
  54         checkFormat(new Long(10), "1.E1");
  55         checkFormat(new Long(1000), "1.E3");
  56         checkFormat(new BigDecimal("0.0"), "0.E0");
  57         checkFormat(new BigDecimal("10.0"), "1.E1");
  58         checkFormat(new BigDecimal("1000.0"), "1.E3");
  59         checkFormat(new BigInteger("00"), "0.E0");
  60         checkFormat(new BigInteger("10"), "1.E1");
  61         checkFormat(new BigInteger("1000"), "1.E3");
  62 
  63         df.setDecimalSeparatorAlwaysShown(false);
  64         checkFormat(new Double(0.0), "0E0");
  65         checkFormat(new Double(10.0), "1E1");
  66         checkFormat(new Double(1000.0), "1E3");
  67         checkFormat(new Long(0), "0E0");
  68         checkFormat(new Long(10), "1E1");
  69         checkFormat(new Long(1000), "1E3");
  70         checkFormat(new BigDecimal("0.0"), "0E0");
  71         checkFormat(new BigDecimal("10.0"), "1E1");
  72         checkFormat(new BigDecimal("1000.0"), "1E3");
  73         checkFormat(new BigInteger("0"), "0E0");
  74         checkFormat(new BigInteger("10"), "1E1");
  75         checkFormat(new BigInteger("1000"), "1E3");
  76 
  77         df.applyPattern("0.###");
  78 
  79         df.setDecimalSeparatorAlwaysShown(true);
  80         checkFormat(new Double(0.0), "0.");
  81         checkFormat(new Double(10.0), "10.");
  82         checkFormat(new Double(1000.0), "1000.");
  83         checkFormat(new Long(0), "0.");
  84         checkFormat(new Long(10), "10.");
  85         checkFormat(new Long(1000), "1000.");
  86         checkFormat(new BigDecimal("0.0"), "0.");
  87         checkFormat(new BigDecimal("10.0"), "10.");
  88         checkFormat(new BigDecimal("1000.0"), "1000.");
  89         checkFormat(new BigInteger("0"), "0.");
  90         checkFormat(new BigInteger("10"), "10.");
  91         checkFormat(new BigInteger("1000"), "1000.");
  92 
  93         df.setDecimalSeparatorAlwaysShown(false);
  94         checkFormat(new Double(0.0), "0");
  95         checkFormat(new Double(10.0), "10");
  96         checkFormat(new Double(1000.0), "1000");
  97         checkFormat(new Long(0), "0");
  98         checkFormat(new Long(10), "10");
  99         checkFormat(new Long(1000), "1000");
 100         checkFormat(new BigDecimal("0.0"), "0");
 101         checkFormat(new BigDecimal("10.0"), "10");
 102         checkFormat(new BigDecimal("1000.0"), "1000");
 103         checkFormat(new BigInteger("0"), "0");
 104         checkFormat(new BigInteger("10"), "10");
 105         checkFormat(new BigInteger("1000"), "1000");
 106 
 107         Locale.setDefault(defaultLoc);
 108 
 109         if (err) {
 110             throw new RuntimeException("Wrong format/parse with DecimalFormat");
 111         }
 112     }
 113 
 114     static void checkFormat(Number num, String expected) {
 115         String got = df.format(num);
 116         if (!got.equals(expected)) {
 117             err = true;
 118             System.err.println("    DecimalFormat format(" +
 119                                num.getClass().getName() +
 120                                ") error:" +
 121                                "\n\tnumber:           " + num +
 122                                "\n\tSeparatorShown? : " + df.isDecimalSeparatorAlwaysShown() +
 123                                "\n\tgot:              " + got +
 124                                "\n\texpected:         " + expected);
 125         }
 126     }
 127 }