Use compareTo(BigDecimal.ZERO) instead of equals():
if (price.compareTo(BigDecimal.ZERO) == 0) // see below
Comparing with the BigDecimal constant BigDecimal.ZERO avoids having to construct a new BigDecimal(0) every execution.
FYI, BigDecimal also has constants BigDecimal.ONE and BigDecimal.TEN for your convenience.
Note!
The reason you can't use BigDecimal#equals() is that it takes scale into consideration:
new BigDecimal("0").equals(BigDecimal.ZERO) // true
new BigDecimal("0.00").equals(BigDecimal.ZERO) // false!
so it's unsuitable for a purely numeric comparison. However, BigDecimal.compareTo() doesn't consider scale when comparing:
new BigDecimal("0").compareTo(BigDecimal.ZERO) == 0 // true
new BigDecimal("0.00").compareTo(BigDecimal.ZERO) == 0 // true
Answer from Bohemian on Stack OverflowUse compareTo(BigDecimal.ZERO) instead of equals():
if (price.compareTo(BigDecimal.ZERO) == 0) // see below
Comparing with the BigDecimal constant BigDecimal.ZERO avoids having to construct a new BigDecimal(0) every execution.
FYI, BigDecimal also has constants BigDecimal.ONE and BigDecimal.TEN for your convenience.
Note!
The reason you can't use BigDecimal#equals() is that it takes scale into consideration:
new BigDecimal("0").equals(BigDecimal.ZERO) // true
new BigDecimal("0.00").equals(BigDecimal.ZERO) // false!
so it's unsuitable for a purely numeric comparison. However, BigDecimal.compareTo() doesn't consider scale when comparing:
new BigDecimal("0").compareTo(BigDecimal.ZERO) == 0 // true
new BigDecimal("0.00").compareTo(BigDecimal.ZERO) == 0 // true
Alternatively, signum() can be used:
if (price.signum() == 0) {
return true;
}
It's as simple as:
if (value.compareTo(BigDecimal.ZERO) > 0)
The documentation for compareTo actually specifies that it will return -1, 0 or 1, but the more general Comparable<T>.compareTo method only guarantees less than zero, zero, or greater than zero for the appropriate three cases - so I typically just stick to that comparison.
Possible better way:
if (value.signum() > 0)
signum returns -1, 0, or 1 as the value of this BigDecimal is negative, zero, or positive.
To be short:
firstBigDecimal.compareTo(secondBigDecimal) < 0 // "<"
firstBigDecimal.compareTo(secondBigDecimal) > 0 // ">"
firstBigDecimal.compareTo(secondBigDecimal) == 0 // "=="
firstBigDecimal.compareTo(secondBigDecimal) != 0 // "!="
firstBigDecimal.compareTo(secondBigDecimal) >= 0 // ">="
firstBigDecimal.compareTo(secondBigDecimal) <= 0 // "<="
Every object of the Class BigDecimal has a method compareTo you can use to compare it to another BigDecimal. The result of compareTo is then compared > 0, == 0 or < 0 depending on what you need. Read the documentation and you will find out.
The operators ==, <, > and so on can only be used on primitive data types like int, long, double or their wrapper classes like Integerand Double.
From the documentation of compareTo:
Compares this
BigDecimalwith the specifiedBigDecimal.Two
BigDecimalobjects that are equal in value but have a different scale (like 2.0 and 2.00) are considered equal by this method. This method is provided in preference to individual methods for each of the six boolean comparison operators (<, ==, >, >=, !=, <=). The suggested idiom for performing these comparisons is:(x.compareTo(y) <op> 0), where<op>is one of the six comparison operators.Returns: -1, 0, or 1 as this BigDecimal is numerically less than, equal to, or greater than val.