Path: blob/master/test/jdk/java/text/Format/NumberFormat/Bug4833877.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 negative multiplier works as expected.26* @bug 483387727*/2829import java.math.*;30import java.text.*;31import java.util.*;3233public class Bug4833877 {3435static DecimalFormat df;3637static boolean err = false;3839public static void main(String[] args) throws Exception {4041Locale defaultLoc = Locale.getDefault();42Locale.setDefault(Locale.US);4344/* ================================================================ */4546df = new DecimalFormat();47df.setMaximumFractionDigits(50);48df.setMultiplier(4);4950/*51* Test for double/Double52*/53checkFormat(252.5252525252525, "1,010.10101010101");54checkParse("-1,010.10101010101", -252.5252525252525);5556checkFormat(-2222.2222, "-8,888.8888");57checkParse("8888.8888", 2222.2222);5859/*60* Test for long/Long61*/62checkFormat(1000L, "4,000");63checkParse("-4,000", -1000L);6465checkFormat(-250L, "-1,000");66checkParse("1000", 250L);6768/* ---------------------------------------------------------------- */6970df.setParseBigDecimal(true);7172/*73* Test for BigDecimal74*/75checkFormat(new BigDecimal("22222.222222222222222222222"),76"88,888.888888888888888888888");77checkParse("-88,888.888888888888888888888",78new BigDecimal("-22222.222222222222222222222"));7980checkFormat(new BigDecimal("-1111111111111111111.111111111111111111"),81"-4,444,444,444,444,444,444.444444444444444444");82checkParse("4444444444444444444.444444444444444444",83new BigDecimal("1111111111111111111.111111111111111111"));8485/*86* Test for BigInteger87*/88checkFormat(new BigInteger("22222222222222222222222222"),89"88,888,888,888,888,888,888,888,888");90checkParse("-88,888,888,888,888,888,888,888,888",91new BigDecimal("-22222222222222222222222222"));9293checkFormat(new BigInteger("-1111111111111111111111111"),94"-4,444,444,444,444,444,444,444,444");95checkParse("4444444444444444444444444",96new BigDecimal("1111111111111111111111111"));9798/* ---------------------------------------------------------------- */99100df.setParseBigDecimal(false);101df.setMultiplier(-4);102103/*104* Test for double/Double105*/106checkFormat(252.5252525252525, "-1,010.10101010101");107checkParse("-1,010.10101010101", 252.5252525252525);108109checkFormat(-2222.2222, "8,888.8888");110checkParse("8888.8888", -2222.2222);111112/*113* Test for long/Long114*/115checkFormat(1000L, "-4,000");116checkParse("-4,000", 1000L);117118checkFormat(-250L, "1,000");119checkParse("1000", -250L);120121/* ---------------------------------------------------------------- */122123df.setParseBigDecimal(true);124125/*126* Test for BigDecimal127*/128checkFormat(new BigDecimal("22222.222222222222222222222"),129"-88,888.888888888888888888888");130checkParse("-88,888.888888888888888888888",131new BigDecimal("22222.222222222222222222222"));132133checkFormat(new BigDecimal("-1111111111111111111.111111111111111111"),134"4,444,444,444,444,444,444.444444444444444444");135checkParse("4444444444444444444.444444444444444444",136new BigDecimal("-1111111111111111111.111111111111111111"));137138/*139* Test for BigInteger140*/141checkFormat(new BigInteger("22222222222222222222222222"),142"-88,888,888,888,888,888,888,888,888");143checkParse("-88,888,888,888,888,888,888,888,888",144new BigDecimal("22222222222222222222222222"));145146checkFormat(new BigInteger("-1111111111111111111111111"),147"4,444,444,444,444,444,444,444,444");148checkParse("4444444444444444444444444",149new BigDecimal("-1111111111111111111111111"));150151/* ---------------------------------------------------------------- */152153df.setParseBigDecimal(false);154df.setMultiplier(-3);155156/*157* Test for double/Double158*/159checkFormat(3333.3333333, "-9,999.9999999");160checkParse("-10,000.00000000000", 3333.3333333333335);// rounding error161162df.setParseIntegerOnly(true);163checkFormat(-3333.3333333, "9,999.9999999");164checkParse("10,000.00000000000", -3333L);165df.setParseIntegerOnly(false);166checkFormat(-3333.3333333, "9,999.9999999");167checkParse("10,000.00000000000", -3333.3333333333335);// rounding error168169/*170* Test for long/Long171*/172checkFormat(3333L, "-9,999");173df.setParseIntegerOnly(true);174checkParse("-10,000", 3333L);175df.setParseIntegerOnly(false);176checkParse("-10000", 3333.3333333333335);// rounding error177178checkFormat(-3333L, "9,999");179df.setParseIntegerOnly(true);180checkParse("10,000", -3333L);181df.setParseIntegerOnly(false);182checkParse("10000", -3333.3333333333335);// rounding error183184/* ---------------------------------------------------------------- */185186df.setParseBigDecimal(true);187188/*189* Test for BigDecimal190*/191checkFormat(new BigDecimal("33333.333333333333333333333"),192"-99,999.999999999999999999999");193checkParse("-100,000.000000000000000000000",194new BigDecimal("33333.333333333333333333333"));195196checkFormat(new BigDecimal("-33333.333333333333333333333"),197"99,999.999999999999999999999");198checkParse("100,000.000000000000000000000",199new BigDecimal("-33333.333333333333333333333"));200201/*202* Test for BigInteger203*/204checkFormat(new BigInteger("33333333333333333333333333"),205"-99,999,999,999,999,999,999,999,999");206checkParse("-100,000,000,000,000,000,000,000,000",207new BigDecimal("33333333333333333333333333"));208209checkFormat(new BigInteger("-33333333333333333333333333"),210"99,999,999,999,999,999,999,999,999");211df.setParseIntegerOnly(true);212checkParse("100,000,000,000,000,000,000,000,000.000",213new BigDecimal("-33333333333333333333333333"));214df.setParseIntegerOnly(false);215checkParse("100,000,000,000,000,000,000,000,000.000",216new BigDecimal("-33333333333333333333333333.333"));217218/* ================================================================ */219220df = new DecimalFormat("0.#E0;-0.#E0");221df.setMaximumFractionDigits(50);222df.setMultiplier(4);223224/*225* Test for double/Double226*/227checkFormat(252.5252525252525, "1.01010101010101E3");228checkParse("-1.01010101010101E3", -2.525252525252525E2);229230checkFormat(-2222.2222, "-8.8888888E3");231checkParse("8888.8888", 2.2222222E3);232233/*234* Test for long/Long235*/236checkFormat(1000L, "4E3");237checkParse("-4E3", -1000L);238239checkFormat(-250L, "-1E3");240checkParse("1000", 250L);241242/* ---------------------------------------------------------------- */243244df.setParseBigDecimal(true);245246/*247* Test for BigDecimal248*/249250checkFormat(new BigDecimal("22222.222222222222222222222"),251"8.8888888888888888888888888E4");252checkParse("-8.8888888888888888888888888E4",253new BigDecimal("-2.2222222222222222222222222E4"));254255checkFormat(new BigDecimal("-1111111111111111111.111111111111111111"),256"-4.444444444444444444444444444444444444E18");257checkParse("4444444444444444444.444444444444444444",258new BigDecimal("1111111111111111111.111111111111111111"));259260/*261* Test for BigInteger262*/263checkFormat(new BigInteger("22222222222222222222222222"),264"8.8888888888888888888888888E25");265checkParse("-8.8888888888888888888888888E25",266new BigDecimal("-22222222222222222222222222"));267268checkFormat(new BigInteger("-1111111111111111111111111"),269"-4.444444444444444444444444E24");270checkParse("4444444444444444444444444",271new BigDecimal("1111111111111111111111111"));272273/* ---------------------------------------------------------------- */274275df.setParseBigDecimal(false);276df.setMultiplier(-4);277278/*279* Test for double/Double280*/281checkFormat(252.5252525252525, "-1.01010101010101E3");282checkParse("-1.01010101010101E3", 2.525252525252525E2);283284checkFormat(-2222.2222, "8.8888888E3");285checkParse("8888.8888", -2.2222222E3);286287/*288* Test for long/Long289*/290checkFormat(1000L, "-4E3");291checkParse("-4E3", 1000L);292293checkFormat(-250L, "1E3");294checkParse("1000", -250L);295296/* ---------------------------------------------------------------- */297298df.setParseBigDecimal(true);299300/*301* Test for BigDecimal302*/303304checkFormat(new BigDecimal("22222.222222222222222222222"),305"-8.8888888888888888888888888E4");306checkParse("-8.8888888888888888888888888E4",307new BigDecimal("2.2222222222222222222222222E4"));308309checkFormat(new BigDecimal("-1111111111111111111.111111111111111111"),310"4.444444444444444444444444444444444444E18");311checkParse("4444444444444444444.444444444444444444",312new BigDecimal("-1111111111111111111.111111111111111111"));313314/*315* Test for BigInteger316*/317checkFormat(new BigInteger("22222222222222222222222222"),318"-8.8888888888888888888888888E25");319checkParse("-8.8888888888888888888888888E25",320new BigDecimal("22222222222222222222222222"));321322checkFormat(new BigInteger("-1111111111111111111111111"),323"4.444444444444444444444444E24");324checkParse("4444444444444444444444444",325new BigDecimal("-1111111111111111111111111"));326327/* ---------------------------------------------------------------- */328329df.setParseBigDecimal(false);330df.setMultiplier(-3);331332/*333* Test for double/Double334*/335checkFormat(3333.3333333, "-9.9999999999E3");336checkParse("-1.00000000000000E3", 3.33333333333333333E2);337338df.setParseIntegerOnly(true);339checkFormat(-3333.3333333, "9.9999999999E3");340checkParse("10.00000000000000E3",-3L);341df.setParseIntegerOnly(false);342checkFormat(-3333.3333333, "9.9999999999E3");343checkParse("10.00000000000000E3", -3.33333333333333333E3);344345/*346* Test for long/Long347*/348checkFormat(3333L, "-9.999E3");349df.setParseIntegerOnly(true);350checkParse("-1.0E4", 0L);351df.setParseIntegerOnly(false);352checkParse("-1.0E4", 3333.3333333333335);353354checkFormat(-3333L, "9.999E3");355df.setParseIntegerOnly(true);356checkParse("10.0E4", -3L);357df.setParseIntegerOnly(false);358checkParse("10.0E4", -33333.3333333333336);359360/* ---------------------------------------------------------------- */361362df.setParseBigDecimal(true);363364/*365* Test for BigDecimal366*/367368checkFormat(new BigDecimal("333.333333333333333333333333"),369"-9.99999999999999999999999999E2");370checkParse("-1.0000000000000000000000000E3",371new BigDecimal("3.333333333333333333333333E2"));372373df.setParseIntegerOnly(true);374checkFormat(new BigDecimal("-333.333333333333333333333333"),375"9.99999999999999999999999999E2");376checkParse("10.0000000000000000000000000E3",377new BigDecimal("-3"));378df.setParseIntegerOnly(false);379checkFormat(new BigDecimal("-333.333333333333333333333333"),380"9.99999999999999999999999999E2");381checkParse("1.0000000000000000000000000E3",382new BigDecimal("-3.333333333333333333333333E2"));383384/*385* Test for BigInteger386*/387checkFormat(new BigInteger("33333333333333333333333333"),388"-9.9999999999999999999999999E25");389checkParse("-100000000000000000000000000",390new BigDecimal("33333333333333333333333333"));391392checkFormat(new BigInteger("-33333333333333333333333333"),393"9.9999999999999999999999999E25");394df.setParseIntegerOnly(true);395checkParse("100000000000000000000000000000",396new BigDecimal("-33333333333333333333333333333"));397df.setParseIntegerOnly(false);398checkParse("100000000000000000000000000.000",399new BigDecimal("-33333333333333333333333333.333"));400401/* ================================================================ */402403Locale.setDefault(defaultLoc);404405if (err) {406throw new RuntimeException("Wrong format/parse with DecimalFormat");407}408}409410static void checkFormat(Number num, String expected) {411String got = df.format(num);412if (!got.equals(expected)) {413err = true;414System.err.println(" DecimalFormat format(" +415num.getClass().getName() +416") error:" +417"\n\tnumber: " + num +418"\n\tpattern: " + df.toPattern() +419"\n\tmultiplier: " + df.getMultiplier() +420"\n\tgot: " + got +421"\n\texpected: " + expected);422}423}424425static void checkParse(String text, Double expected) {426Double got = (Double)df.parse(text, new ParsePosition(0));427if (!got.equals(expected)) {428err = true;429System.err.println(" DecimalFormat parse(double) error:" +430"\n\ttext: " + text +431"\n\tpattern: " + df.toPattern() +432"\n\tmultiplier: " + df.getMultiplier() +433"\n\tgot: " + got +434"\n\texpected: " + expected);435}436}437438static void checkParse(String text, Long expected) {439Long got = (Long)df.parse(text, new ParsePosition(0));440if (!got.equals(expected)) {441err = true;442System.err.println(" DecimalFormat parse(long) error:" +443"\n\ttext: " + text +444"\n\tpattern: " + df.toPattern() +445"\n\tmultiplier: " + df.getMultiplier() +446"\n\tgot: " + got +447"\n\texpected: " + expected);448}449}450451static void checkParse(String text, BigDecimal expected) {452BigDecimal got = (BigDecimal)df.parse(text, new ParsePosition(0));453if (!got.equals(expected)) {454err = true;455System.err.println(" DecimalFormat parse(BigDecimal) error:" +456"\n\ttext: " + text +457"\n\tpattern: " + df.toPattern() +458"\n\tmultiplier: " + df.getMultiplier() +459"\n\tgot: " + got +460"\n\texpected: " + expected);461}462}463}464465466