-
Type:
Bug
-
Resolution: Duplicate
-
Priority:
P3
-
None
-
Affects Version/s: 22, 25
-
Component/s: core-libs
-
generic
A DESCRIPTION OF THE PROBLEM :
A DecimalFormat with a mask containing percentage sign does not respect the rounding mode HALF_UP. This only happens when rounding up from zero to one. See reproduction.
REGRESSION : Last worked in version 21.0.9
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
String mask = "#%";
DecimalFormat df = new DecimalFormat();
df.setParseBigDecimal(true);
df.setRoundingMode(RoundingMode.HALF_UP);
df.applyPattern(mask);
System.out.println(df.format(new BigDecimal("0.005"))); //Outputs 0% (should be 1%)
System.out.println(df.format(new BigDecimal("0.015"))); //Outputs 2%
System.out.println(df.format(new BigDecimal("0.025"))); //Outputs 3%
---------- BEGIN SOURCE ----------
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;
public class DecimalFormatPercentageAroundZero {
public static void main(String[] args) {
String mask = "#%";
DecimalFormat df = new DecimalFormat();
df.setParseBigDecimal(true);
df.setRoundingMode(RoundingMode.HALF_UP);
df.applyPattern(mask);
System.out.println(df.format(new BigDecimal("0.005"))); //Outputs 0% (should be 1%)
System.out.println(df.format(new BigDecimal("0.015"))); //Outputs 2%
System.out.println(df.format(new BigDecimal("0.025"))); //Outputs 3%
}
//Java21 ok
//Java22 and up not ok
}
---------- END SOURCE ----------
FREQUENCY :
ALWAYS
A DecimalFormat with a mask containing percentage sign does not respect the rounding mode HALF_UP. This only happens when rounding up from zero to one. See reproduction.
REGRESSION : Last worked in version 21.0.9
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
String mask = "#%";
DecimalFormat df = new DecimalFormat();
df.setParseBigDecimal(true);
df.setRoundingMode(RoundingMode.HALF_UP);
df.applyPattern(mask);
System.out.println(df.format(new BigDecimal("0.005"))); //Outputs 0% (should be 1%)
System.out.println(df.format(new BigDecimal("0.015"))); //Outputs 2%
System.out.println(df.format(new BigDecimal("0.025"))); //Outputs 3%
---------- BEGIN SOURCE ----------
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;
public class DecimalFormatPercentageAroundZero {
public static void main(String[] args) {
String mask = "#%";
DecimalFormat df = new DecimalFormat();
df.setParseBigDecimal(true);
df.setRoundingMode(RoundingMode.HALF_UP);
df.applyPattern(mask);
System.out.println(df.format(new BigDecimal("0.005"))); //Outputs 0% (should be 1%)
System.out.println(df.format(new BigDecimal("0.015"))); //Outputs 2%
System.out.println(df.format(new BigDecimal("0.025"))); //Outputs 3%
}
//Java21 ok
//Java22 and up not ok
}
---------- END SOURCE ----------
FREQUENCY :
ALWAYS
- duplicates
-
JDK-8369050 DecimalFormat Rounding Errors for Fractional Ties Near Zero
-
- Resolved
-