I needed to round floating point numbers half up to the nearest 1/100 decimal place:
123.045 = 123.05
Plus I need to eliminate any floating point representation errors. I'm currently doing this:
double val = x*y;
val= new BigDecimal(val, MathContext.DECIMAL64).setScale(2, BigDecimal.ROUND_HALF_UP)
This works for all my test cases but my confidence in this has dropped after realizing Mathcontexts DECIMAL128 and UNLIMITED inexplicably didnt evaluate one test case correctly.
Is there a more fail proof method to do this? thanks
123.045 = 123.05
Plus I need to eliminate any floating point representation errors. I'm currently doing this:
double val = x*y;
val= new BigDecimal(val, MathContext.DECIMAL64).setScale(2, BigDecimal.ROUND_HALF_UP)
This works for all my test cases but my confidence in this has dropped after realizing Mathcontexts DECIMAL128 and UNLIMITED inexplicably didnt evaluate one test case correctly.
Is there a more fail proof method to do this? thanks