Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4133597

DecimalFormat broken in 1.1.6 -- adds 2 preceeding 0's to format pattern

XMLWordPrintable

    • generic, x86
    • generic, windows_95, windows_nt

      Name: dm26566 Date: 04/29/98

      public static void main(String[] args)
      {
        String pattern = "#0.00";
        DecimalFormat f = new DecimalFormat(pattern);
        System.out.println("new pattern: " +
                           f.toPattern());
      }

      This prints "#000.0" and "006.74". This used to work perfectly in 1.1.5, but now it doesn't.

      Additional Notes from dalem:

      This is also a probelm on JDK-1.2beta4-C.
      It used to print out "#0.00" in 1.1.5
      (Review ID: 29322)
      ======================================================================

      Another test case from another customer:

      When using JDK 1.1.6 the values on the screen don't look like the given format.

      Example:

      import java.text.DecimalFormat;
      import java.text.FieldPosition;
      import java.text.ParsePosition;

      public class NiceFormat extends DecimalFormat
      {
          /** Value for an empty Double. */
          private static final Double EMPTY_DOUBLE = new Double (Double.NaN);

          /** The format supported by this class */
          private static final String STANDARD_FORMAT = "#0.000";

          /** Singleton for NiceFormat */
          private static NiceFormat standardFormat;

      /** Constructor calls the Super-Class with STANDARD_FORMAT */
      private NiceFormat ()
      {
      super (STANDARD_FORMAT);
      }

          /**
      * returns the one and only instance for this class
      *
           * @return NiceFormat the instance of this class.
           */
          public static NiceFormat getNiceFormat ()
      {
              if (standardFormat == null)
                  standardFormat = new NiceFormat ();

              return standardFormat;
          }

          /**
      * returns the formatted text from the given double
      *
           * @param aNumber the number to format.
           * @param aResult the stringbuffer holding the result.
           * @param aFieldPosition position where parsing ends.
      * @returns StringBuffer the formatted text.
           */
          public StringBuffer format (double aNumber, StringBuffer aResult,
      FieldPosition aFieldPosition)
      {
               if (Double.isNaN (aNumber))
                  return aResult;
               else
               {
                  return super.format (aNumber, aResult, aFieldPosition);
               }
          }

          /**
      * methode to parse a String
      *
           * @param aText text to be parsed.
      * @returns Number the value represented by the given text.
           */
          public Number parse (String aText)
      {
              return parse (aText, new ParsePosition (0));
          }

          /**
      * parse the String and return the value, if String is empty, return
      * the value <code>EMPTY_DOUBLE</code>
      *
           * @param aText text to be parsed.
      * @param aFieldPosition position, where parsing ends.
      * @returns Number the value represented by the given text.
           */
          public Number parse (String aSource, ParsePosition aFieldPosition)
      {
              // delete all spaces.
              String source = aSource.trim ();

              // return EMPTY_DOUBLE, if String is empty.
              if (source.length () == 0)
                  return EMPTY_DOUBLE;
              else
                  return super.parse (aSource,aFieldPosition);
          }

      public static void main (String someArguments [])
      {
      NiceFormat theFormat = NiceFormat.getNiceFormat ();
      double theValue = 4.070;

      System.out.println ("Value: " + theValue + ", formatted: " + theFormat.format (theValue));
      }
      }

      I expected an output like:
      Value: 4.07, formatted: 4,070

      but I got:
      Value: 4.07, formatted: 0004,070

            aliusunw Alan Liu (Inactive)
            dmcduffisunw Dale Mcduffie (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: