Long time i didn’t write blog post in English. I’m afraid my skill on this will slowly fade away, so here i go, switching to English 😀 .In this article i’m gonna explain about how to compare BigDecimal value in Java. For those who already have an intermediate skill in Java it’s not a big deal, but it is for newcomers. This article won’t take your precious time and won’t make you have “too long;didn’t read” feeling.

When comparing primitive data type we will definitely use comparison operator which are “” and others. Now, try using those operators to compare BigDecimal value and tell me what you got.

1

2

3

4

5

6

BigDecimal seven=newBigDecimal(7);

BigDecimal ten=newBigDecimal(10);

if(seven<ten){

}

Yep, annoying red will show up

Short conclusion : we can’t use those operators to compare BigDecimal value.
Solution : use compareTo method

This method will return :-1 if method caller is less then the parameter value0 if method caller is equal then the parameter value1 if method caller is greater then the parameter value