JDK-8186431 : DecimalFormat suffix stomps decimal separator when fraction pattern is optional
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.text
  • Affected Version: 8
  • Priority: P4
  • Status: Resolved
  • Resolution: Duplicate
  • OS: generic
  • CPU: generic
  • Submitted: 2017-08-16
  • Updated: 2017-08-21
  • Resolved: 2017-08-21
Related Reports
Duplicate :  
Description
FULL PRODUCT VERSION :
java version "1.8.0_144"
Java(TM) SE Runtime Environment (build 1.8.0_144-b01)
Java HotSpot(TM) 64-Bit Server VM (build 25.144-b01, mixed mode)

ADDITIONAL OS VERSION INFORMATION :
Microsoft Windows [Version 10.0.14393]

A DESCRIPTION OF THE PROBLEM :
DecimalFormat's fastPath doesn't reapply the decimal separator after stomping on it with the suffix.

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Create a DecimalFormat with a suffix, e.g. "#,###.###':'"  

Use it to format a number that has no decimal component.

Use it to format a number with a decimal component.



EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
128:
0.03:
0.03:
true
ACTUAL -
128:
0:03:
0.03:
true

Note the colon in the decimal position of the second line.


REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------

import java.text.DecimalFormat;

public class DecimalFormatBug
{
    public static void main(String[] args) {
        String f = "#,###.###':'";
        DecimalFormat df1 = new DecimalFormat(f);
        DecimalFormat df2 = new DecimalFormat(f);
        System.out.println(df1.format(128.0));
        System.out.println(df1.format(0.03));
        System.out.println(df2.format(0.03));
        System.out.println(df1.equals(df2));
    }
}

---------- END SOURCE ----------

CUSTOMER SUBMITTED WORKAROUND :
Don't use a suffix in the DecimalFormat or require a fractional digit, e.g. "#,###.0##':'";



Comments
This bug is a duplicate of JDK-8165466. jdk8u backport will be opened for the main bug.
21-08-2017

I think the issue is similar to JDK-8165466, which is resolved in JDK9-b139.
18-08-2017

To reproduce the issue, run the attached test case. JDK 8 - Fail JDK 8u144 - Fail JDK 9-ea +180 - Pass Following is the output on failed versions: 128: 0:03: 0.03: true Following is the output on JDK 9: 128: 0.03: 0.03: true
18-08-2017