JDK-6609682 : [Fmt-De] DecimalFormat constructor imposes default negative prefix when none specified
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.text
  • Affected Version: 5.0
  • Priority: P4
  • Status: Resolved
  • Resolution: Not an Issue
  • OS: windows_xp
  • CPU: x86
  • Submitted: 2007-09-26
  • Updated: 2017-09-05
  • Resolved: 2017-09-05
Related Reports
Relates :  
Description
FULL PRODUCT VERSION :
java version "1.5.0_02"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_02-b09)
Java HotSpot(TM) Client VM (build 1.5.0_02-b09, mixed mode, sharing)


ADDITIONAL OS VERSION INFORMATION :
This definitely occurs under Windows XP (5.1.2600) and Linux  (2.4.20-8)

A DESCRIPTION OF THE PROBLEM :
The javadoc class comment for DecimalFormat states that the negative subpattern is optional, but that if it is supplied, it is used to determine the negative prefix and negative suffix.

If I use a DecimalFormat constructor that supplies a negative subpattern with no negative prefix nor negative suffix, I get a default negative prefix (-) applied to my strings, instead of no prefix as I would expect.


STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Run the supplied source code

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
23.23
23.23

ACTUAL -
-23.23
-23.23


REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
import java.text.*;
import java.math.*;

class test {
        public static void main(String[] argv) throws Exception {
                DecimalFormat df = new DecimalFormat("#,##0.##;#,##0.##");
                System.out.println(df.format(new Double(-23.23)));
                System.out.println(df.format(new BigDecimal("-23.23")));
        }
}

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

Comments
The specification of DecimalFormat states that "If there is an explicit negative subpattern, it serves only to specify the negative prefix and suffix;". In the above given example since there is not prefix and suffix given with the negative subpattern, the negative subpattern is ignored or considered absent and as per specification "the positive subpattern prefixed with the localized minus sign ('-' in most locales) is used as the negative subpattern."
05-09-2017

Possibly related to the improper handling of negative subpattern
22-02-2017