How to compare BigDecimal and Double in Java
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