Code example for BigDecimal

Methods: scaleunscaledValue

0
        int precision = 21;
        RoundingMode rm = RoundingMode.CEILING;
        MathContext mc = new MathContext(precision, rm);
        String res = "732546982374982285074";
        int resScale = -15;
        BigDecimal result = new BigDecimal(a, mc);
        assertEquals("incorrect value", res, result.unscaledValue().toString());
        assertEquals("incorrect scale", resScale, result.scale());
 
        // Now test more than just RoundingMode.CEILING 
        // 
        BigDecimal bd;
 
        mc = new MathContext(9, RoundingMode.UP);
        bd = new BigDecimal(123456789.125, mc);
        assertEquals("incorrect value",  "123456790", bd.toString());
        bd = new BigDecimal(-123456789.125, mc);
        assertEquals("incorrect value", "-123456790", bd.toString());
 
        mc = new MathContext(8, RoundingMode.DOWN);
Experience pair programming with AI  Get Codota for Java