Skip to content

Commit 6971c23

Browse files
Ian GravesBrent Christian
Ian Graves
authored and
Brent Christian
committedMar 10, 2021
8262351: Extra '0' in java.util.Formatter for '%012a' conversion with a sign character
Reviewed-by: bchristi, naoto
1 parent c6d74bd commit 6971c23

File tree

2 files changed

+56
-2
lines changed

2 files changed

+56
-2
lines changed
 

‎src/java.base/share/classes/java/util/Formatter.java

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2003, 2020, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2003, 2021, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -3554,7 +3554,12 @@ else if (precision == 0)
35543554
sb.append(upper ? "0X" : "0x");
35553555

35563556
if (f.contains(Flags.ZERO_PAD)) {
3557-
trailingZeros(sb, width - s.length() - 2);
3557+
int leadingCharacters = 2;
3558+
if(f.contains(Flags.LEADING_SPACE) ||
3559+
f.contains(Flags.PLUS) || neg) {
3560+
leadingCharacters = 3;
3561+
}
3562+
trailingZeros(sb, width - s.length() - leadingCharacters);
35583563
}
35593564

35603565
int idx = s.indexOf('p');
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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 8262351
27+
* @summary Incorrect zero padding occurs in the presence of some leading sign
28+
* flags for hexadecimal floating point conversions.
29+
* @run testng HexFloatZeroPadding
30+
*/
31+
32+
import org.testng.annotations.Test;
33+
import static org.testng.Assert.*;
34+
35+
@Test
36+
public class HexFloatZeroPadding {
37+
38+
public void testCorrectWidthHexFloatZeroPadding()
39+
{
40+
final int EXPECTEDLENGTH = 12;
41+
assertEquals(String.format("|%010a|", 0.0).length(), EXPECTEDLENGTH);
42+
assertEquals(String.format("|% 010a|", 0.0).length(), EXPECTEDLENGTH);
43+
assertEquals(String.format("|%+010a|", 0.0).length(), EXPECTEDLENGTH);
44+
assertEquals(String.format("|%010a|", -0.0).length(), EXPECTEDLENGTH);
45+
assertEquals(String.format("|% 010a|", -0.0).length(), EXPECTEDLENGTH);
46+
assertEquals(String.format("|%+010a|", -0.0).length(), EXPECTEDLENGTH);
47+
}
48+
49+
}

0 commit comments

Comments
 (0)