Path: blob/master/test/jdk/java/text/Format/NumberFormat/Bug4208135.java
41152 views
/*1* Copyright (c) 2003, 2016, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @summary Confirm that the decimal separator is shown when explicitly requested.26* @bug 420813527*/2829import java.math.*;30import java.text.*;31import java.util.*;3233public class Bug4208135 {3435static DecimalFormat df;3637static boolean err = false;3839static public void main(String[] args){4041Locale defaultLoc = Locale.getDefault();42Locale.setDefault(Locale.US);4344df = new DecimalFormat();4546df.applyPattern("0.#E0");4748df.setDecimalSeparatorAlwaysShown(true);49checkFormat(0.0, "0.E0");50checkFormat(10.0, "1.E1");51checkFormat(1000.0, "1.E3");52checkFormat(0L, "0.E0");53checkFormat(10L, "1.E1");54checkFormat(1000L, "1.E3");55checkFormat(new BigDecimal("0.0"), "0.E0");56checkFormat(new BigDecimal("10.0"), "1.E1");57checkFormat(new BigDecimal("1000.0"), "1.E3");58checkFormat(new BigInteger("00"), "0.E0");59checkFormat(new BigInteger("10"), "1.E1");60checkFormat(new BigInteger("1000"), "1.E3");6162df.setDecimalSeparatorAlwaysShown(false);63checkFormat(0.0, "0E0");64checkFormat(10.0, "1E1");65checkFormat(1000.0, "1E3");66checkFormat(0L, "0E0");67checkFormat(10L, "1E1");68checkFormat(1000L, "1E3");69checkFormat(new BigDecimal("0.0"), "0E0");70checkFormat(new BigDecimal("10.0"), "1E1");71checkFormat(new BigDecimal("1000.0"), "1E3");72checkFormat(new BigInteger("0"), "0E0");73checkFormat(new BigInteger("10"), "1E1");74checkFormat(new BigInteger("1000"), "1E3");7576df.applyPattern("0.###");7778df.setDecimalSeparatorAlwaysShown(true);79checkFormat(0.0, "0.");80checkFormat(10.0, "10.");81checkFormat(1000.0, "1000.");82checkFormat(0L, "0.");83checkFormat(10L, "10.");84checkFormat(1000L, "1000.");85checkFormat(new BigDecimal("0.0"), "0.");86checkFormat(new BigDecimal("10.0"), "10.");87checkFormat(new BigDecimal("1000.0"), "1000.");88checkFormat(new BigInteger("0"), "0.");89checkFormat(new BigInteger("10"), "10.");90checkFormat(new BigInteger("1000"), "1000.");9192df.setDecimalSeparatorAlwaysShown(false);93checkFormat(0.0, "0");94checkFormat(10.0, "10");95checkFormat(1000.0, "1000");96checkFormat(0L, "0");97checkFormat(10L, "10");98checkFormat(1000L, "1000");99checkFormat(new BigDecimal("0.0"), "0");100checkFormat(new BigDecimal("10.0"), "10");101checkFormat(new BigDecimal("1000.0"), "1000");102checkFormat(new BigInteger("0"), "0");103checkFormat(new BigInteger("10"), "10");104checkFormat(new BigInteger("1000"), "1000");105106Locale.setDefault(defaultLoc);107108if (err) {109throw new RuntimeException("Wrong format/parse with DecimalFormat");110}111}112113static void checkFormat(Number num, String expected) {114String got = df.format(num);115if (!got.equals(expected)) {116err = true;117System.err.println(" DecimalFormat format(" +118num.getClass().getName() +119") error:" +120"\n\tnumber: " + num +121"\n\tSeparatorShown? : " + df.isDecimalSeparatorAlwaysShown() +122"\n\tgot: " + got +123"\n\texpected: " + expected);124}125}126}127128129