Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/util/Formatter/BigDecimalRounding.java
41149 views
1
/*
2
* Copyright (c) 2021, 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 8262744
27
* @summary BigDecimal does not always display formatting correctly because
28
* rounding is done after formatting check. Fix moves rounding to before the
29
* range-based formatting check for %g formatting flag.
30
* @run testng BigDecimalRounding
31
*/
32
33
import org.testng.annotations.Test;
34
35
import java.math.BigDecimal;
36
37
import static org.testng.Assert.*;
38
39
@Test
40
public class BigDecimalRounding {
41
42
public static void testBigDecimalRounding() {
43
var res1 = String.format("%g", 0.00009999999999999995);
44
var res2 = String.format("%g", 0.00009999999f);
45
var res3 = String.format("%g", new BigDecimal(0.0001));
46
var res4 = String.format("%g", new BigDecimal("0.00009999999999999999995"));
47
48
assertEquals(res1, res2);
49
assertEquals(res2, res3);
50
assertEquals(res3, res4);
51
52
var res5 = String.format("%.9g", 999999.999999432168754e+3);
53
var res6 = String.format("%.9g", 999999999.999432168754f);
54
var res7 = String.format("%.9g", new BigDecimal("999999.999999432168754e+3")); // !!
55
var res8 = String.format("%.9g", new BigDecimal("1000000000")); // !!
56
57
assertEquals(res5, res6);
58
assertEquals(res6, res7);
59
assertEquals(res7, res8);
60
61
}
62
}
63
64