Code example for MathContext

Methods: hashCode, toString

0
	public void testMathContextConstruction() { 
        String a = "-12380945E+61";
        BigDecimal aNumber = new BigDecimal(a);
        int precision = 6;
        RoundingMode rm = RoundingMode.HALF_DOWN;
        MathContext mcIntRm = new MathContext(precision, rm);
        MathContext mcStr = new MathContext("precision=6 roundingMode=HALF_DOWN");
        MathContext mcInt = new MathContext(precision);
        BigDecimal res = aNumber.abs(mcInt);
        assertEquals("MathContext Constructer with int precision failed",
                res, 
                new BigDecimal("1.23809E+68"));
         
        assertEquals("Equal MathContexts are not Equal ",
                mcIntRm,
                mcStr);
         
        assertEquals("Different MathContext are reported as Equal ",
        		mcInt.equals(mcStr),
                false); 
         
        assertEquals("Equal MathContexts have different hashcodes ",
                mcIntRm.hashCode(),
                mcStr.hashCode());
        
        assertEquals("MathContext.toString() returning incorrect value",
                mcIntRm.toString(),
                "precision=6 roundingMode=HALF_DOWN"); 
	} 
 
}