Compare if BigDecimal is greater than zero

不打扰是莪最后的温柔 提交于 2019-12-03 02:07:56

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!