Code example for MathContext

0
 
    public void testConstrDoubleMathContext_AndroidFailure() { 
        BigDecimal bd;
        MathContext mc;
 
        mc = new MathContext(11, RoundingMode.HALF_EVEN);
        // 
        // VERY FUNNY: 
        // This works: 
        bd = new BigDecimal("123456789.125", mc);
        assertEquals("incorrect value",  "123456789.12", bd.toString());
        // But this doesn't: 
        bd = new BigDecimal(123456789.125, mc);
        assertEquals("incorrect value",  "123456789.12", bd.toString());
 
        bd = new BigDecimal(-123456789.125, mc);
        assertEquals("incorrect value", "-123456789.12", bd.toString());
 
        // AND HERE, TOO: 
        mc = new MathContext(11, RoundingMode.HALF_UP);
        bd = new BigDecimal(123456789.125, mc);