JDK-8193920 : Decimal format with positive suffix breaks after formatting zero
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.text
  • Affected Version: 8u151
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • OS: windows
  • CPU: x86_64
  • Submitted: 2017-12-18
  • Updated: 2017-12-21
  • Resolved: 2017-12-21
Related Reports
Duplicate :  
Description
FULL PRODUCT VERSION :
java version "1.8.0_151"
Java(TM) SE Runtime Environment (build 1.8.0_151-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.151-b12, mixed mode)

ADDITIONAL OS VERSION INFORMATION :
Windows 10 Pro, v1709, OS Build 16299.64

A DESCRIPTION OF THE PROBLEM :
When using a DecimalFormat with a positive suffix, the format method returns correctly until the first time the value 0.0 is passed. At this point, the decimal separator is removed and  replaced by the first character of the specified suffix. The cause is down to how the fastFormat data is handled. When 0.0 is passed, the decimal separator is overwritten with the suffix in the FastPathData instance, and is never re-introduced later.

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Create a new NumberFormat instance by calling the NumberFormat.getInstance() method (Other constructors can be used, so long as the resulting instance matches the fast formatting rules in the DecimalFormat class). 

Cast the instance to a DecimalFormat instance, and set the positive suffix to a non empty string, e.g. "ft".

Call format on a fractional value greater than one, e.g. 1.5 and display the result.

Call format on passing zero and display the result.

Call format on a fractional value greater than one, e.g. 1.5 and display the result.




EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
The expected output is;

1.5ft
0ft
1.5ft
ACTUAL -
The actual result is;
1.5ft
0ft
1f5ft

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
NumberFormat format = NumberFormat.getInstance();
((DecimalFormat) format).setPositiveSuffix("ft");
System.out.println(format.format(1.5));
System.out.println(format.format(0.0));
System.out.println(format.format(1.5));
---------- END SOURCE ----------

CUSTOMER SUBMITTED WORKAROUND :
None found.


Comments
This is fixed in JDK 9 and backported to JDK 8u172.
21-12-2017