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

java.text.DecimalFormat.format(double d) incorrectly works with -0.0

    XMLWordPrintable

    Details

    • Subcomponent:
    • Resolved In Build:
      1.2fcs
    • CPU:
      sparc
    • OS:
      solaris_2.5
    • Verification:
      Verified

      Description



      Name: dfC67450 Date: 06/11/98



      The java.text.DecimalFormat.format(double d) treats -0.0 as the positive number.
      But following the principles of float and double type -0.0 and +0.0 are the
      different numbers and should be processed in the differnt ways. Javadoc should
      clarify it.

      Here is the test demonstrating the bug:

      -----------------TestD.java------------------------
      import java.text.*;
      import java.util.*;


      public class TestD {

          public static void main (String args[]){
              DecimalFormat df = new DecimalFormat("#,##0.0##");
              df.setDecimalFormatSymbols(new DecimalFormatSymbols(Locale.ENGLISH));
              double d1 = -0.0;
              double d2 = -0.0001;
              StringBuffer f1 = df.format(d1, new StringBuffer(), new FieldPosition(0));
              StringBuffer f2 = df.format(d2, new StringBuffer(), new FieldPosition(0));
              if ( !f1.equals("-0.0") || !f2.equals("-0.0")) {
                System.out.println("Test failed");
                System.out.println("pattern: \"" + df.toPattern() + "\"");
                System.out.println(d1 + " is formatted as " + f1);
                System.out.println(d2 + " is formatted as " + f2);
                System.out.println("This numbers should be formatted as -0.0");
              } else {
                System.out.println("Test passed");
              }
          }
      }
      ---------Output from the test---------------------
      Test failed
      pattern: "#,##0.0##"
      -0.0 is formatted as 0.0
      -1.0E-4 is formatted as 0.0
      This numbers should be formatted as -0.0
      --------------------------------------------------

      ======================================================================

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              aliusunw Alan Liu (Inactive)
              Reporter:
              dfazunensunw Dmitri Fazunenko (Inactive)
              Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved:
                Imported:
                Indexed: