Comparing BigDecimal Value in Java


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.

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 value
0 if method caller is equal then the parameter value
1 if method caller is greater then the parameter value

Here’s the snippet that might help you for better understanding

And the result

It is self-explained i guess, isn’t it? 😀
Happy coding 🙂

Facebook Comments

Agung Setiawan

Agung Setiawan adalah software engineer di, penulis sekaligus pecinta sastra, dan pembaca buku

Halo, perkenalkan saya Agung Setiawan.
Seorang Software Engineer di BukaLapak, penulis buku, blogger, gunpla builder, tech educator.
Simak pemikiran saya soal dunia Software Engineering via Twitter di @agungsetiawanmu dan facebook
Mau belajar Vim bareng saya?
Belajar ngoding dari nol menggunakan PHP

Leave a Reply

Your email address will not be published. Required fields are marked *