JDK-7021311 : Inconsistent behaviour of BigDecimal.stripTrailingZeros()
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.math
  • Affected Version: 6
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • OS: windows_xp
  • CPU: x86
  • Submitted: 2011-02-22
  • Updated: 2012-03-20
  • Resolved: 2011-08-24
Related Reports
Duplicate :  
Description
FULL PRODUCT VERSION :
java version "1.6.0_17"
Java(TM) SE Runtime Environment (build 1.6.0_17-b04)
Java HotSpot(TM) Client VM (build 14.3-b01, mixed mode, sharing)

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

A DESCRIPTION OF THE PROBLEM :
Calling stripTrailingZeros() e.g. on value "1.00" leads to "1" as expected, but for value "0.00" (or "0.0" etc.) it always returns the given value without stripping the trailing zeros.


STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
(new BigDecimal("1.00")).stripTrailingZeros()
//Result: 1

(new BigDecimal("1.10")).stripTrailingZeros()
//Result: 1.1

(new BigDecimal("0.00")).stripTrailingZeros()
//Result: 0.00

(new BigDecimal("0.0")).stripTrailingZeros()
//Result: 0.0

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
(new BigDecimal("0.00")).stripTrailingZeros()
//Expected result: 0

(new BigDecimal("0.0")).stripTrailingZeros()
//Expected result: 0

ACTUAL -
(new BigDecimal("0.00")).stripTrailingZeros()
//Result: 0.00

(new BigDecimal("0.0")).stripTrailingZeros()
//Result: 0.0

REPRODUCIBILITY :
This bug can be reproduced always.

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

public class StripTrailingZerosTest {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// for value 1.00 the behaviour is as expected:
		BigDecimal bd = new BigDecimal("1.00");
		System.out.println("stripTrailingZeros on 1.00: " + bd.stripTrailingZeros().toString());
		
		// for value 0.00 it is not:
		BigDecimal bdZero = new BigDecimal("0.00");
		System.out.println("stripTrailingZeros on 0.00: " + bdZero.stripTrailingZeros().toString());
	}

}

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

Comments
EVALUATION Closing as duplicate of 6480539.
24-08-2011