In this example Java bigdecimal class divide (BigDecimal divisor, MathContext mc) method working is demonstrated. This method divides this.object value by the specified object value & returns quotient as per defined context settings. Scale of the quotient is this.scale, which is the scale of the object in which the method is invoked.
Example contains three MathContext class objects mc_0, mc_1 and mc_2 respectively and three different MathContext class static fields DECIMAL32, DECIMAL64 & DECIMAL128 respectively. Static fields used are MathContext class, predefined objects that have their own format and rounding mode settings defined in them. Method throws Arithmetic Exception in following cases :
If the Rounding mode is UNNECESSARY, ie mc.precision == 0, when the divisor value is zero, when the specified rounding mode does not represent a valid rounding mode, and when a non-terminating quotient is generated.
Method throws NumberFormatException if it finds a value other than a integer or double value.
Syntax for using the method:
System.out.println(bigdecimal_objectName.divide(BigDecimal divisor, MathContext
BigDecimal x = this.object.divide(divisor, mc);
Posted on: November 21, 2008 If you enjoyed this post then why not add us on Google+? Add us to your Circles