Path: blob/master/test/jdk/java/text/Format/NumberFormat/DFSExponential.java
41152 views
/*1* Copyright (c) 2005, 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* @bug 406806726* @library /java/text/testlib27* @summary test NumberFormat with exponential separator symbols. It also tests the new28* public methods in DecimalFormatSymbols, setExponentSeparator() and29* getExponentSeparator()30*/3132import java.util.*;33import java.text.*;3435public class DFSExponential extends IntlTest36{3738public static void main(String[] args) throws Exception {39new DFSExponential().run(args);40}414243public void DFSExponenTest() throws Exception {44DecimalFormatSymbols sym = new DecimalFormatSymbols(Locale.US);45String pat[] = { "0.####E0", "00.000E00", "##0.####E000", "0.###E0;[0.###E0]" };46double val[] = { 0.01234, 123456789, 1.23e300, -3.141592653e-271 };47long lval[] = { 0, -1, 1, 123456789 };48String valFormat[][] = {49{"1.234x10^-2", "1.2346x10^8", "1.23x10^300", "-3.1416x10^-271"},50{"12.340x10^-03", "12.346x10^07", "12.300x10^299", "-31.416x10^-272"},51{"12.34x10^-003", "123.4568x10^006", "1.23x10^300", "-314.1593x10^-273"},52{"1.234x10^-2", "1.235x10^8", "1.23x10^300", "[3.142x10^-271]"},53};545556int ival = 0, ilval = 0;57logln("Default exponent separator: "+sym.getExponentSeparator());58try {59sym.setExponentSeparator("x10^");60} catch (NullPointerException e){61errln("null String was passed to set an exponent separator symbol");62throw new RuntimeException("Test Malfunction: null String was passed to set an exponent separator symbol" );63}64logln("Current exponent separator: "+sym.getExponentSeparator());6566for (int p=0; p<pat.length; ++p){67DecimalFormat fmt = new DecimalFormat(pat[p], sym);68logln(" Pattern: " + fmt.toPattern());69String locPattern = fmt.toLocalizedPattern();70logln(" Localized pattern: "+locPattern);71//fmt.applyLocalizedPattern(locPattern);72//System.out.println(" fmt.applyLocalizedPattern(): "+fmt.toLocalizedPattern());7374for (int v=0; v<val.length; ++v) {75String s = fmt.format(val[v]);76logln(" " + val[v]+" --> "+s);77if(valFormat[p][v].equals(s)){78logln(": Passed");79}else{80errln(" Failed: Should be formatted as "+valFormat[p][v]+ "but got "+s);81throw new RuntimeException(" Failed: Should be formatted as "+valFormat[p][v]+ "but got "+s);82}83}84} //end of the first for loop85}86}878889