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 * @bug 4944439 27 * @summary Confirm that numbers where all digits after the decimal separator are 0 28 * and which are between Long.MIN_VALUE and Long.MAX_VALUE are returned as Long(not double). 29 */ 30 31 import java.math.BigDecimal; 32 import java.math.BigInteger; 33 import java.text.DecimalFormat; 34 import java.util.Locale; 35 36 public class Bug4944439 { 37 38 static boolean err = false; 39 static DecimalFormat df; 40 41 public static void main(String[] args) throws Exception { 42 43 Locale defaultLoc = Locale.getDefault(); 44 Locale.setDefault(Locale.US); 45 46 df = new DecimalFormat(); 47 String s = "-9223372036854775809"; // Long.MIN_VALUE-1 48 check_Double(s); 49 50 test(Long.MIN_VALUE, Long.MIN_VALUE+10); 51 test(-10, 10); 52 test(Long.MAX_VALUE-10, Long.MAX_VALUE-1); 53 54 s = "9223372036854775807.00"; // Long.MAX_VALUE 55 check_Long(s); 56 s = "9223372036854775808"; // Long.MAX_VALUE+1 57 check_Double(s); 58 59 s = "-0.0"; 60 check_Double(s); 61 s = "0.0"; 62 check_Long(s); 63 64 Locale.setDefault(defaultLoc); 65 66 if (err) { 67 throw new RuntimeException("Wrong parsing with DecimalFormat"); 68 } 69 } 70 71 private static void test(long from, long to) throws Exception { 72 for (long l = from; l <= to; l++) { 73 check_Long(Long.toString(l) + ".00"); 74 } 75 } 76 77 private static void check_Long(String s) throws Exception { 78 Number number = df.parse(s); 79 if (!(number instanceof Long)) { 80 err = true; 81 System.err.println("Failed: DecimalFormat.parse(\"" + s + 82 "\") should return a Long, but returned a " + 83 number.getClass().getName()); 84 } 85 86 int index = s.indexOf('.'); 87 Long l = new Long(s.substring(0, index)); 88 if (!l.equals(number)) { 89 err = true; 90 System.err.println("Failed: DecimalFormat.parse(" + s + 91 ") should return a Long(" + l + "), but returned " + number); 92 } 93 } 94 95 private static void check_Double(String s) throws Exception { 96 Number number = df.parse(s); 97 if (!(number instanceof Double)) { 98 err = true; 99 System.err.println("Failed: DecimalFormat.parse(\"" + s + 100 "\") should return a Double, but returned a " + 101 number.getClass().getName()); 102 } 103 104 Double d = new Double(s); 105 if (!d.equals(number)) { 106 err = true; 107 System.err.println("Failed: DecimalFormat.parse(" + s + 108 ") should return a Double(" + d + "), but returned " + number); 109 } 110 } 111 }