Code example for MathContext

0
    public void testConstrBigIntegerMathContext() { 
        String a = "1231212478987482988429808779810457634781384756794987";
        BigInteger bA = new BigInteger(a);
        int precision = 46;
        RoundingMode rm = RoundingMode.CEILING;
        MathContext mc = new MathContext(precision, rm);
        String res = "1231212478987482988429808779810457634781384757";
        int resScale = -6;
        BigDecimal result = new BigDecimal(bA, mc);
        assertEquals("incorrect value", res, result.unscaledValue().toString());
        assertEquals("incorrect scale", resScale, result.scale());
 
        // Now test more than just RoundingMode.CEILING: 
        // 
        BigDecimal bd;
        BigInteger bi =  new BigInteger( "12345678901234567890123456789012345");
        BigInteger nbi = new BigInteger("-12345678901234567890123456789012345");
 
        mc = new MathContext(31, RoundingMode.UP);
        bd = new BigDecimal(bi, mc);
        assertEquals("incorrect value",  "1.234567890123456789012345678902E+34", bd.toString());