Software Development, Linux, and Windows

Notes on Software Development, Linux, and Windows

July, 2013

Compare BigDecimal to Zero in Java

You really need to use compareTo() instead of equals() for this: equals() yields inconsistent results with decimal values: new BigDecimal( 0 ).equals( BigDecimal.ZERO ); // true new BigDecimal( 0.00 ).equals( BigDecimal.ZERO ); // false As you can see, eqauls() gives unexpected results. compareTo() does this right thing: new BigDecimal( 0 ).compareTo( BigDecimal.ZERO ) == 0; [...]

Posted in Java | Comments Off