JDK-6480539 : BigDecimal.stripTrailingZeros() has no effect on zero itself ("0.0")
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.math
  • Affected Version: 5.0,6
  • Priority: P4
  • Status: Closed
  • Resolution: Fixed
  • OS: windows_xp
  • CPU: x86
  • Submitted: 2006-10-11
  • Updated: 2017-05-16
  • Resolved: 2013-07-10
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availabitlity Release.

To download the current JDK release, click here.
JDK 8
8 b100Fixed
Related Reports
Duplicate :  
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();
}

Comments
Could not find anything on grepcode.com or code.google.com which fixing this to return BigDecimal.ZERO for magnitude 0 would break.
2013-07-03

EVALUATION Will investigate.
2006-10-11