Possible Duplicate:
ArithmeticException thrown during BigDecimal.divide
This results in ArithmeticException: http://ideone.com/RXeZw
Providing a scale and rounding mode will give me the wrong result. This example should output 50.03%. How to round this correctly?
For easier reference, this is the code:
BigDecimal priceDiff = BigDecimal.ONE
.subtract(new BigDecimal(9.99)
.divide(new BigDecimal(19.99)))
.multiply(new BigDecimal(100));
System.out.println(priceDiff.toPlainString());
BigDecimal.divide(BigDecimal divisor)
throws an ArithmeticException if the result cannot be exactly represented.
You will have to use provide a MathContext or a RoundingMode telling how you want to handle this. For example:
public class Test {
public static void main(String[] args) throws java.lang.Exception {
BigDecimal priceDiff = BigDecimal.ONE.subtract(new BigDecimal("9.99").divide(new BigDecimal("19.99"), MathContext.DECIMAL128))
.multiply(new BigDecimal(100));
System.out.println(priceDiff.toPlainString());
}
}
works and prints
50.0250125062531265632816408204102100
Also, note the use of the BigDecimal(String)
constructor to avoid problems when you create BigDecimal
using a double
literal.
You should use a MathContext as your division as an infinite number of decimals:
BigDecimal priceDiff = BigDecimal.ONE
.subtract(new BigDecimal(9.99)
.divide(new BigDecimal(19.99), new MathContext(10, RoundingMode.UP)))
.multiply(new BigDecimal(100));
System.out.println(priceDiff.toPlainString());
However, that prints 50.025...
This would print 49.98:
BigDecimal priceDiff = new BigDecimal(9.99)
.divide(new BigDecimal(19.99), new MathContext(10, RoundingMode.UP))
.multiply(new BigDecimal(100), new MathContext(4, RoundingMode.UP));
System.out.println(priceDiff.toPlainString());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With