How to use comparison operators like >, =, < on BigDecimal Ask Question

How to use comparison operators like >, =, < on BigDecimal Ask Question

I have a domain class with unitPrice set as BigDecimal data type. Now I am trying to create a method to compare price but it seems like I can't have comparison operators in BigDecimal data type. Do I have to change data type or is there other way around?

ベストアンサー1

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  // "<="    

おすすめ記事