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?
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
- Difference between Types.INTEGER and Types.NULL in
BigDecimal
isn't a primitive, so you cannot use the<
,>
operators. However, since it's aComparable
, you can use thecompareTo(BigDecimal)
to the same effect. E.g.:You can use method named
compareTo
,x.compareTo(y)
. It will return 0 if x and y are equal, 1 if x is greater than y and -1 if x is smaller than yUse the
compareTo
method of BigDecimal :Every object of the Class
BigDecimal
has a methodcompareTo
you can use to compare it to another BigDecimal. The result ofcompareTo
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 likeint
,long
,double
or their wrapper classes likeInteger
andDouble
.From the documentation of
compareTo
:To be short:
Here is an example for all six boolean comparison operators (<, ==, >, >=, !=, <=):