Behavior change in the BigDecimal stripTrailingZeros method for a zero value

Java 8 introduces a behavior change in the java.math.BigDecimal stripTrailingZeros method when the value is numerically equal to zero and has a nonzero scale. Scale is the number of zeros to the right of the decimal point. Prior to Java 8, the stripTrailingZeros method returned the value itself. In Java 8, the method returns BigDecimal.ZERO, which is the value zero with a zero scale.

This rule flags the java.math.BigDecimal stripTrailingZeros method. No code change is required for this behavior change, which now produces the expected behavior of the method. As you prepare to use Java 8, be aware of this difference because it might cause test cases to fail.

The following example shows the use of the stripTrailingZeros method.

public static void printResults(BigDecimal dec) {

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());
}

public static void main(String args[]) {

BigDecimal dec = new BigDecimal("0.000000");
printResults(dec);
}

If you call this code using Java 7, the following output is displayed:

Starting decimal = 0.000000 scale = 6
Ending decimal = 0.000000 scale = 6

In Java 8, the following output is displayed:

Starting decimal = 0.000000 scale = 6
Ending decimal = 0 scale = 0

For more information about the BigDecimal method, see the java.math.BigDecimal Java documentation.