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

后端 未结 8 1188
清酒与你
清酒与你 2021-01-30 11:54

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 Big

8条回答
  •  北恋
    北恋 (楼主)
    2021-01-30 12:50

    BigDecimal isn't a primitive, so you cannot use the <, > operators. However, since it's a Comparable, you can use the compareTo(BigDecimal) to the same effect. E.g.:

    public class Domain {
        private BigDecimal unitPrice;
    
        public boolean isCheaperThan(BigDecimal other) {
            return unitPirce.compareTo(other.unitPrice) < 0;
        }
    
        // etc...
    }
    

提交回复
热议问题