BigDecimal division error problem

https://zhidao.baidu.com/question/477740409.html

My idea is that BigDecimal has unlimited precision, so when doing division, the computer doesn't know how much precision the result needs, so if you don't specify it, it will report an error

MathContext mc = new MathContext(2, RoundingMode.HALF_DOWN);
//The precision is 2, the rounding mode is greater than 0.5 and 1, otherwise it is discarded.
BigDecimal a = new BigDecimal(0.5);
BigDecimal b = new BigDecimal(0.2);
System.out.println(a.divide(b,mc));

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327041423&siteId=291194637