问题
How can I compare if BigDecimal
value is greater than zero?
回答1:
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.
回答2:
Possible better way:
if (value.signum() > 0)
回答3:
Use compareTo()
function that's built into the class.
回答4:
using ".intValue()" on BigDecimal object is not right when you want to check if its grater than zero. The only option left is ".compareTo()" method.
回答5:
BigDecimal obj = new BigDecimal("100");
if(obj.intValue()>0)
System.out.println("yes");
来源:https://stackoverflow.com/questions/4164521/compare-if-bigdecimal-is-greater-than-zero