Path: blob/master/test/jdk/java/text/Format/NumberFormat/Bug4944439.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* @bug 494443926* @summary Confirm that numbers where all digits after the decimal separator are 027* and which are between Long.MIN_VALUE and Long.MAX_VALUE are returned as Long(not double).28*/2930import java.math.BigDecimal;31import java.math.BigInteger;32import java.text.DecimalFormat;33import java.util.Locale;3435public class Bug4944439 {3637static boolean err = false;38static DecimalFormat df;3940public static void main(String[] args) throws Exception {4142Locale defaultLoc = Locale.getDefault();43Locale.setDefault(Locale.US);4445df = new DecimalFormat();46String s = "-9223372036854775809"; // Long.MIN_VALUE-147check_Double(s);4849test(Long.MIN_VALUE, Long.MIN_VALUE+10);50test(-10, 10);51test(Long.MAX_VALUE-10, Long.MAX_VALUE-1);5253s = "9223372036854775807.00"; // Long.MAX_VALUE54check_Long(s);55s = "9223372036854775808"; // Long.MAX_VALUE+156check_Double(s);5758s = "-0.0";59check_Double(s);60s = "0.0";61check_Long(s);6263Locale.setDefault(defaultLoc);6465if (err) {66throw new RuntimeException("Wrong parsing with DecimalFormat");67}68}6970private static void test(long from, long to) throws Exception {71for (long l = from; l <= to; l++) {72check_Long(Long.toString(l) + ".00");73}74}7576private static void check_Long(String s) throws Exception {77Number number = df.parse(s);78if (!(number instanceof Long)) {79err = true;80System.err.println("Failed: DecimalFormat.parse(\"" + s +81"\") should return a Long, but returned a " +82number.getClass().getName());83}8485int index = s.indexOf('.');86Long l = Long.valueOf(s.substring(0, index));87if (!l.equals(number)) {88err = true;89System.err.println("Failed: DecimalFormat.parse(" + s +90") should return a Long(" + l + "), but returned " + number);91}92}9394private static void check_Double(String s) throws Exception {95Number number = df.parse(s);96if (!(number instanceof Double)) {97err = true;98System.err.println("Failed: DecimalFormat.parse(\"" + s +99"\") should return a Double, but returned a " +100number.getClass().getName());101}102103Double d = Double.valueOf(s);104if (!d.equals(number)) {105err = true;106System.err.println("Failed: DecimalFormat.parse(" + s +107") should return a Double(" + d + "), but returned " + number);108}109}110}111112113