Skip to content

Commit e149bd3

Browse files
author
Justin Lu
committed
8352755: Misconceptions about j.text.DecimalFormat digits during parsing
Reviewed-by: naoto
1 parent e7ce661 commit e149bd3

File tree

3 files changed

+79
-90
lines changed

3 files changed

+79
-90
lines changed

src/java.base/share/classes/java/text/CompactNumberFormat.java

Lines changed: 17 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2018, 2024, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2018, 2025, 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
@@ -2169,14 +2169,10 @@ private void readObject(ObjectInputStream inStream) throws IOException,
21692169
}
21702170

21712171
/**
2172-
* Sets the maximum number of digits allowed in the integer portion of a
2173-
* number.
2174-
* The maximum allowed integer range is 309, if the {@code newValue} > 309,
2175-
* then the maximum integer digits count is set to 309. Negative input
2176-
* values are replaced with 0.
2177-
*
2178-
* @param newValue the maximum number of integer digits to be shown
2179-
* @see #getMaximumIntegerDigits()
2172+
* {@inheritDoc NumberFormat}
2173+
* <p>The maximum allowed integer range is 309, if the {@code newValue} &gt;
2174+
* 309, then the maximum integer digits count is set to 309.
2175+
* @param newValue the maximum number of integer digits to be shown.
21802176
*/
21812177
@Override
21822178
public void setMaximumIntegerDigits(int newValue) {
@@ -2194,14 +2190,10 @@ public void setMaximumIntegerDigits(int newValue) {
21942190
}
21952191

21962192
/**
2197-
* Sets the minimum number of digits allowed in the integer portion of a
2198-
* number.
2199-
* The maximum allowed integer range is 309, if the {@code newValue} &gt; 309,
2200-
* then the minimum integer digits count is set to 309. Negative input
2201-
* values are replaced with 0.
2202-
*
2203-
* @param newValue the minimum number of integer digits to be shown
2204-
* @see #getMinimumIntegerDigits()
2193+
* {@inheritDoc NumberFormat}
2194+
* <p>The maximum allowed integer range is 309, if the {@code newValue} &gt;
2195+
* 309, then the minimum integer digits count is set to 309.
2196+
* @param newValue the minimum number of integer digits to be shown.
22052197
*/
22062198
@Override
22072199
public void setMinimumIntegerDigits(int newValue) {
@@ -2219,14 +2211,10 @@ public void setMinimumIntegerDigits(int newValue) {
22192211
}
22202212

22212213
/**
2222-
* Sets the minimum number of digits allowed in the fraction portion of a
2223-
* number.
2224-
* The maximum allowed fraction range is 340, if the {@code newValue} &gt; 340,
2225-
* then the minimum fraction digits count is set to 340. Negative input
2226-
* values are replaced with 0.
2227-
*
2228-
* @param newValue the minimum number of fraction digits to be shown
2229-
* @see #getMinimumFractionDigits()
2214+
* {@inheritDoc NumberFormat}
2215+
* <p>The maximum allowed fraction range is 340, if the {@code newValue} &gt;
2216+
* 340, then the minimum fraction digits count is set to 340.
2217+
* @param newValue the minimum number of fraction digits to be shown.
22302218
*/
22312219
@Override
22322220
public void setMinimumFractionDigits(int newValue) {
@@ -2245,14 +2233,10 @@ public void setMinimumFractionDigits(int newValue) {
22452233
}
22462234

22472235
/**
2248-
* Sets the maximum number of digits allowed in the fraction portion of a
2249-
* number.
2250-
* The maximum allowed fraction range is 340, if the {@code newValue} &gt; 340,
2251-
* then the maximum fraction digits count is set to 340. Negative input
2252-
* values are replaced with 0.
2253-
*
2254-
* @param newValue the maximum number of fraction digits to be shown
2255-
* @see #getMaximumFractionDigits()
2236+
* {@inheritDoc NumberFormat}
2237+
* <p>The maximum allowed fraction range is 340, if the {@code newValue} &gt;
2238+
* 340, then the maximum fraction digits count is set to 340.
2239+
* @param newValue the maximum number of fraction digits to be shown.
22562240
*/
22572241
@Override
22582242
public void setMaximumFractionDigits(int newValue) {

src/java.base/share/classes/java/text/DecimalFormat.java

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,10 @@
110110
* defined by {@link Character#digit Character.digit}, are recognized.
111111
*
112112
* <h3 id="digit_limits"> Integer and Fraction Digit Limits </h3>
113+
* The integer and fraction digit limits are set by either applying a {@link ##patterns
114+
* pattern} or using one of the appropriate {@code DecimalFormat} setter methods,
115+
* for example, {@link #setMinimumFractionDigits(int)}. These limits have no impact
116+
* on parsing behavior.
113117
* @implSpec
114118
* When formatting a {@code Number} other than {@code BigInteger} and
115119
* {@code BigDecimal}, {@code 309} is used as the upper limit for integer digits,
@@ -3972,9 +3976,9 @@ else if (ch == percent) {
39723976
}
39733977

39743978
/**
3975-
* Sets the maximum number of digits allowed in the integer portion of a
3976-
* number. Negative input values are replaced with 0.
3977-
* @see NumberFormat#setMaximumIntegerDigits
3979+
* {@inheritDoc NumberFormat}
3980+
* @param newValue the maximum number of integer digits to be shown.
3981+
* @see #getMaximumIntegerDigits()
39783982
* @see ##digit_limits Integer and Fraction Digit Limits
39793983
*/
39803984
@Override
@@ -3989,9 +3993,9 @@ public void setMaximumIntegerDigits(int newValue) {
39893993
}
39903994

39913995
/**
3992-
* Sets the minimum number of digits allowed in the integer portion of a
3993-
* number. Negative input values are replaced with 0.
3994-
* @see NumberFormat#setMinimumIntegerDigits
3996+
* {@inheritDoc NumberFormat}
3997+
* @param newValue the minimum number of integer digits to be shown.
3998+
* @see #getMinimumIntegerDigits()
39953999
* @see ##digit_limits Integer and Fraction Digit Limits
39964000
*/
39974001
@Override
@@ -4006,9 +4010,9 @@ public void setMinimumIntegerDigits(int newValue) {
40064010
}
40074011

40084012
/**
4009-
* Sets the maximum number of digits allowed in the fraction portion of a
4010-
* number. Negative input values are replaced with 0.
4011-
* @see NumberFormat#setMaximumFractionDigits
4013+
* {@inheritDoc NumberFormat}
4014+
* @param newValue the maximum number of fraction digits to be shown.
4015+
* @see #getMaximumFractionDigits()
40124016
* @see ##digit_limits Integer and Fraction Digit Limits
40134017
*/
40144018
@Override
@@ -4023,9 +4027,9 @@ public void setMaximumFractionDigits(int newValue) {
40234027
}
40244028

40254029
/**
4026-
* Sets the minimum number of digits allowed in the fraction portion of a
4027-
* number. Negative input values are replaced with 0.
4028-
* @see NumberFormat#setMinimumFractionDigits
4030+
* {@inheritDoc NumberFormat}
4031+
* @param newValue the minimum number of fraction digits to be shown.
4032+
* @see #getMinimumFractionDigits()
40294033
* @see ##digit_limits Integer and Fraction Digit Limits
40304034
*/
40314035
@Override
@@ -4040,11 +4044,11 @@ public void setMinimumFractionDigits(int newValue) {
40404044
}
40414045

40424046
/**
4043-
* Gets the maximum number of digits allowed in the integer portion of a
4044-
* number. The maximum number of integer digits can be set by either {@link #setMaximumIntegerDigits(int)}
4045-
* or {@link #applyPattern(String)}. See the {@link ##patterns Pattern Section} for
4046-
* comprehensive rules regarding maximum integer digits in patterns.
4047+
* {@inheritDoc NumberFormat}
4048+
* <p>Unlike the other digit limits, {@code maximumIntegerDigits} is not
4049+
* updated by {@code DecimalFormats} created or updated with a string pattern.
40474050
* @see #setMaximumIntegerDigits
4051+
* @see ##patterns Pattern Section
40484052
* @see ##digit_limits Integer and Fraction Digit Limits
40494053
*/
40504054
@Override
@@ -4053,8 +4057,7 @@ public int getMaximumIntegerDigits() {
40534057
}
40544058

40554059
/**
4056-
* Gets the minimum number of digits allowed in the integer portion of a
4057-
* number.
4060+
* {@inheritDoc NumberFormat}
40584061
* @see #setMinimumIntegerDigits
40594062
* @see ##digit_limits Integer and Fraction Digit Limits
40604063
*/
@@ -4064,8 +4067,7 @@ public int getMinimumIntegerDigits() {
40644067
}
40654068

40664069
/**
4067-
* Gets the maximum number of digits allowed in the fraction portion of a
4068-
* number.
4070+
* {@inheritDoc NumberFormat}
40694071
* @see #setMaximumFractionDigits
40704072
* @see ##digit_limits Integer and Fraction Digit Limits
40714073
*/
@@ -4075,8 +4077,7 @@ public int getMaximumFractionDigits() {
40754077
}
40764078

40774079
/**
4078-
* Gets the minimum number of digits allowed in the fraction portion of a
4079-
* number.
4080+
* {@inheritDoc NumberFormat}
40804081
* @see #setMinimumFractionDigits
40814082
* @see ##digit_limits Integer and Fraction Digit Limits
40824083
*/

src/java.base/share/classes/java/text/NumberFormat.java

Lines changed: 39 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -129,9 +129,9 @@
129129
* <ul>
130130
* <li> {@link #setParseIntegerOnly(boolean)}; when {@code true}, will only return the
131131
* integer portion of the number parsed from the String.
132-
* <li> {@link #setMinimumFractionDigits(int)}; Use to adjust the expected digits when
133-
* formatting. Use any of the other minimum/maximum or fraction/integer setter methods
134-
* in the same manner.
132+
* <li> {@link #setMinimumFractionDigits(int)}; Use to adjust the expected digits
133+
* when formatting. Use any of the other minimum/maximum or fraction/integer
134+
* setter methods in the same manner. These methods have no impact on parsing behavior.
135135
* <li> {@link #setGroupingUsed(boolean)}; when {@code true}, formatted numbers will be displayed
136136
* with grouping separators. Additionally, when {@code false}, parsing will not expect
137137
* grouping separators in the parsed String.
@@ -918,7 +918,7 @@ public void setGroupingUsed(boolean newValue) {
918918

919919
/**
920920
* Returns the maximum number of digits allowed in the integer portion of a
921-
* number.
921+
* number during formatting.
922922
*
923923
* @return the maximum number of digits
924924
* @see #setMaximumIntegerDigits
@@ -929,14 +929,15 @@ public int getMaximumIntegerDigits() {
929929

930930
/**
931931
* Sets the maximum number of digits allowed in the integer portion of a
932-
* number. maximumIntegerDigits must be &ge; minimumIntegerDigits. If the
933-
* new value for maximumIntegerDigits is less than the current value
934-
* of minimumIntegerDigits, then minimumIntegerDigits will also be set to
935-
* the new value.
932+
* number during formatting. {@code maximumIntegerDigits} must be &ge;
933+
* {@code minimumIntegerDigits}. If the new value for {@code
934+
* maximumIntegerDigits} is less than the current value of
935+
* {@code minimumIntegerDigits}, then {@code minimumIntegerDigits} will
936+
* also be set to the new value. Negative input values are replaced with 0.
936937
*
937-
* @param newValue the maximum number of integer digits to be shown; if
938-
* less than zero, then zero is used. The concrete subclass may enforce an
939-
* upper limit to this value appropriate to the numeric type being formatted.
938+
* @param newValue the maximum number of integer digits to be shown. The
939+
* concrete subclass may enforce an upper limit to this value appropriate to
940+
* the numeric type being formatted.
940941
* @see #getMaximumIntegerDigits
941942
*/
942943
public void setMaximumIntegerDigits(int newValue) {
@@ -948,7 +949,7 @@ public void setMaximumIntegerDigits(int newValue) {
948949

949950
/**
950951
* Returns the minimum number of digits allowed in the integer portion of a
951-
* number.
952+
* number during formatting.
952953
*
953954
* @return the minimum number of digits
954955
* @see #setMinimumIntegerDigits
@@ -959,14 +960,15 @@ public int getMinimumIntegerDigits() {
959960

960961
/**
961962
* Sets the minimum number of digits allowed in the integer portion of a
962-
* number. minimumIntegerDigits must be &le; maximumIntegerDigits. If the
963-
* new value for minimumIntegerDigits exceeds the current value
964-
* of maximumIntegerDigits, then maximumIntegerDigits will also be set to
965-
* the new value
963+
* number during formatting. {@code minimumIntegerDigits} must be &le;
964+
* {@code maximumIntegerDigits}. If the new value for {@code minimumIntegerDigits}
965+
* exceeds the current value of {@code maximumIntegerDigits}, then {@code
966+
* maximumIntegerDigits} will also be set to the new value. Negative input
967+
* values are replaced with 0.
966968
*
967-
* @param newValue the minimum number of integer digits to be shown; if
968-
* less than zero, then zero is used. The concrete subclass may enforce an
969-
* upper limit to this value appropriate to the numeric type being formatted.
969+
* @param newValue the minimum number of integer digits to be shown. The
970+
* concrete subclass may enforce an upper limit to this value appropriate to
971+
* the numeric type being formatted.
970972
* @see #getMinimumIntegerDigits
971973
*/
972974
public void setMinimumIntegerDigits(int newValue) {
@@ -978,7 +980,7 @@ public void setMinimumIntegerDigits(int newValue) {
978980

979981
/**
980982
* Returns the maximum number of digits allowed in the fraction portion of a
981-
* number.
983+
* number during formatting.
982984
*
983985
* @return the maximum number of digits.
984986
* @see #setMaximumFractionDigits
@@ -989,14 +991,15 @@ public int getMaximumFractionDigits() {
989991

990992
/**
991993
* Sets the maximum number of digits allowed in the fraction portion of a
992-
* number. maximumFractionDigits must be &ge; minimumFractionDigits. If the
993-
* new value for maximumFractionDigits is less than the current value
994-
* of minimumFractionDigits, then minimumFractionDigits will also be set to
995-
* the new value.
994+
* number during formatting. {@code maximumFractionDigits} must be &ge;
995+
* {@code minimumFractionDigits}. If the new value for {@code maximumFractionDigits}
996+
* is less than the current value of {@code minimumFractionDigits}, then
997+
* {@code minimumFractionDigits} will also be set to the new value. Negative
998+
* input values are replaced with 0.
996999
*
997-
* @param newValue the maximum number of fraction digits to be shown; if
998-
* less than zero, then zero is used. The concrete subclass may enforce an
999-
* upper limit to this value appropriate to the numeric type being formatted.
1000+
* @param newValue the maximum number of fraction digits to be shown. The
1001+
* concrete subclass may enforce an upper limit to this value appropriate to
1002+
* the numeric type being formatted.
10001003
* @see #getMaximumFractionDigits
10011004
*/
10021005
public void setMaximumFractionDigits(int newValue) {
@@ -1008,7 +1011,7 @@ public void setMaximumFractionDigits(int newValue) {
10081011

10091012
/**
10101013
* Returns the minimum number of digits allowed in the fraction portion of a
1011-
* number.
1014+
* number during formatting.
10121015
*
10131016
* @return the minimum number of digits
10141017
* @see #setMinimumFractionDigits
@@ -1019,14 +1022,15 @@ public int getMinimumFractionDigits() {
10191022

10201023
/**
10211024
* Sets the minimum number of digits allowed in the fraction portion of a
1022-
* number. minimumFractionDigits must be &le; maximumFractionDigits. If the
1023-
* new value for minimumFractionDigits exceeds the current value
1024-
* of maximumFractionDigits, then maximumFractionDigits will also be set to
1025-
* the new value
1025+
* number during formatting. {@code minimumFractionDigits} must be &le;
1026+
* {@code maximumFractionDigits}. If the new value for {@code
1027+
* minimumFractionDigits} exceeds the current value of {@code
1028+
* maximumFractionDigits}, then {@code maximumFractionDigits} will also be
1029+
* set to the new value. Negative input values are replaced with 0.
10261030
*
1027-
* @param newValue the minimum number of fraction digits to be shown; if
1028-
* less than zero, then zero is used. The concrete subclass may enforce an
1029-
* upper limit to this value appropriate to the numeric type being formatted.
1031+
* @param newValue the minimum number of fraction digits to be shown. The
1032+
* concrete subclass may enforce an upper limit to this value appropriate to
1033+
* the numeric type being formatted.
10301034
* @see #getMinimumFractionDigits
10311035
*/
10321036
public void setMinimumFractionDigits(int newValue) {

0 commit comments

Comments
 (0)