Path: blob/master/test/jdk/java/util/Formatter/BigDecimalRounding.java
41149 views
/*1* Copyright (c) 2021, 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 826274426* @summary BigDecimal does not always display formatting correctly because27* rounding is done after formatting check. Fix moves rounding to before the28* range-based formatting check for %g formatting flag.29* @run testng BigDecimalRounding30*/3132import org.testng.annotations.Test;3334import java.math.BigDecimal;3536import static org.testng.Assert.*;3738@Test39public class BigDecimalRounding {4041public static void testBigDecimalRounding() {42var res1 = String.format("%g", 0.00009999999999999995);43var res2 = String.format("%g", 0.00009999999f);44var res3 = String.format("%g", new BigDecimal(0.0001));45var res4 = String.format("%g", new BigDecimal("0.00009999999999999999995"));4647assertEquals(res1, res2);48assertEquals(res2, res3);49assertEquals(res3, res4);5051var res5 = String.format("%.9g", 999999.999999432168754e+3);52var res6 = String.format("%.9g", 999999999.999432168754f);53var res7 = String.format("%.9g", new BigDecimal("999999.999999432168754e+3")); // !!54var res8 = String.format("%.9g", new BigDecimal("1000000000")); // !!5556assertEquals(res5, res6);57assertEquals(res6, res7);58assertEquals(res7, res8);5960}61}626364