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

BigDecimal.stripTrailingZeros() has no effect on zero itself ("0.0")

    XMLWordPrintable

Details

    • b100
    • x86
    • windows_xp
    • Verified

    Description

      FULL PRODUCT VERSION :
      java version "1.5.0_06"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_06-b05)
      Java HotSpot(TM) Client VM (build 1.5.0_06-b05, mixed mode)

      ADDITIONAL OS VERSION INFORMATION :
      Microsoft Windows XP [Version 5.1.2600]

      A DESCRIPTION OF THE PROBLEM :
      BigDecimal.stripTrailingZeros() had no effect on padded versions of 0, such as "0.0000"

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Run the included program with "java -classpath . BigDecimalTest 0.000000"
      or "java -classpath . BigDecimalTest .000000"

      You can use any number in the command line, and it works as expected as long as there is at least one non-zero digit in the input.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Starting decimal = 0.000000 scale = 6
      Ending decimal = 0 scale = 0

      ACTUAL -
      Starting decimal = 0.000000 scale = 6
      Ending decimal = 0.000000 scale = 6

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import java.math.BigDecimal;

      public class BigDecimalTest {

          public static void main(String args[]) {
              BigDecimal dec = new BigDecimal(args[0]);
              System.out.print("Starting decimal = " + dec.toPlainString());
              System.out.println(" scale = " + dec.scale());
              dec = dec.stripTrailingZeros();
              System.out.print("Ending decimal = " + dec.toPlainString());
              System.out.println(" scale = " + dec.scale());

          }
      }

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

      CUSTOMER SUBMITTED WORKAROUND :
      you can work around it with the something like the following:

      BigDecimal zero = new BigDecimal("0");
      if ( someBigDecimal.compareTo(zero) == 0 ) {
          someBigDecimal = zero;
      } else {
          someBigDecimal = someBigDecimal .stripTrailingZeros();
      }

      Attachments

        Issue Links

          Activity

            People

              bpb Brian Burkhalter
              ndcosta Nelson Dcosta (Inactive)
              Votes:
              0 Vote for this issue
              Watchers:
              4 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:
                Imported:
                Indexed: