JDK-6594713 : [Fmt-De] DecimalFormat incorrectly formats values if the negative subpattern is (#)
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.text
  • Affected Version: 1.4.2,5.0,6
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • OS: linux,windows_xp
  • CPU: x86
  • Submitted: 2007-08-20
  • Updated: 2011-02-16
  • Resolved: 2007-10-22
Related Reports
Duplicate :  
Duplicate :  
Duplicate :  
Duplicate :  
Description
FULL PRODUCT VERSION :
java version "1.6.0_02"
Java(TM) SE Runtime Environment (build 1.6.0_02-b05)
Java HotSpot(TM) 64-Bit Server VM (build 1.6.0_02-b05, mixed mode)


ADDITIONAL OS VERSION INFORMATION :
Linux romanov 2.6.22-gentoo-r2 #1 SMP Mon Jul 30 10:23:34 CEST 2007 x86_64 Intel(R) Pentium(R) D CPU 2.80GHz GenuineIntel GNU/Linux

A DESCRIPTION OF THE PROBLEM :
The Javadoc of DecimalFormat states the following:

If there is an explicit negative subpattern, it serves only to specify the negative prefix and suffix; the number of digits, minimal digits, and other characteristics are all the same as the positive pattern. That means that "#,##0.0#;(#)" produces precisely the same behavior as "#,##0.0#;(#,##0.0#)".

If you actually compare the output of DecimalFormat using the patterns given as examples in the Javadoc "#,##0.0#;(#)" and "#,##0.0#;(#,##0.0#)", the output of the shorter pattern is missing the last ')' character.

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
This bug can be reproduced by executing the two lines of code, specified in the source code section of this bug report. This will print formatted text to the stardard output stream. The second printed line will contain an incorrectly formatted value.

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
The following is expected in the standard output:

Long format: '(1.23)'
Short format: '(1.23)'

ACTUAL -
The following is what you actually see in the standard output:

Long format: '(1.23)'
Short format: '(1.23'


REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
System.out.println("Long format: '" + new DecimalFormat("#,##0.0#;(#,##0.0#)").format(-1.23) + "'");
System.out.println("Short format: '" + new DecimalFormat("#,##0.0#;(#)").format(-1.23) + "'");
---------- END SOURCE ----------

CUSTOMER SUBMITTED WORKAROUND :
You can use the full syntax when specifying the negative subpattern.

Comments
EVALUATION Duplicate of 4395163.
22-10-2007