How to compare BigDecimal and Double in Java

close up shot of a casino roulette

Comparing Bigdecimal and Double is tricky because the scale(numbers to the left of .) is higher for BigDecimal as compared to Double.

So you’ll have to decide up to which scale the numbers should be the same to consider both the variables as equal. Once you have decided the limit say 5, you need to multiply both your variables with 10^(your chosen limit) i.e. 10^5 = 100000, and then use a truncation function like Ciel or Floor to ignore the rest of the numbers.

Below is an example code demonstrating the approach:-

Math.ceil(bigDecimalVariable * 100000) == Math.ceil(doubleVariable * 100000);

6 comments

comments user
Hairstyles

whoah this blog is excellent i like studying your articles. Stay up the good paintings! You recognize, lots of individuals are searching around for this info, you can help them greatly.

comments user
dawnofdynastymodwin

Hi there, everything is going nicely here and ofcourse every one is sharing information, that’s really excellent, keep up writing.|

comments user
Myron

I constantly emailed this web site post page to all my friends, since if like to read it next my contacts will too.

comments user
Alberto

Asking questions are really pleasant thing if you are not understanding
something fully, but this piece of writing presents nice understanding even.

comments user
Kellye

Good information. Lucky me I ran across your website by chance (stumbleupon).
I’ve book marked it for later!

comments user
Brenna

Appreciating the commitment you put into your website and in depth information you provide.
It’s great to come across a blog every once in a while that isn’t the same old rehashed information. Great read!
I’ve saved your site and I’m including your RSS feeds to my Google account.

Leave a Reply to Hairstyles Cancel reply