Java MathContext Class Methods with Examples

In this article, we will discuss important methods or APIs of the Java MathContext class from the java.math package.
MathContext objects are immutable which encapsulate the context settings which describe certain rules for numerical operators, such as those implemented by the BigDecimal class. The base-independent settings are:
  • precision: the number of digits to be used for operation; results are rounded to this precision
  • roundingMode: a RoundingMode object which specifies the algorithm to be used for rounding.

Java MathContext Class Diagram

The below class diagram shows a list of methods that the MathContext class provides.

Java MathContext Class Methods/APIs with Examples

Java MathContext Class Methods/APIs

  • boolean equals(Object x) - Compares this MathContext with the specified Object for equality.
  • int getPrecision() - Returns the precision setting.
  • RoundingMode getRoundingMode() - Returns the roundingMode setting.
  • int hashCode() - Returns the hash code for this MathContext.
  • String toString() - Returns the string representation of this MathContext.

Java MathContext Class Methods/APIs Examples

Let's demonstrates the usage of a few important and commonly used MathContext class methods with examples. In below MathContextMethodsExample class, each method name describes the MathContext class method and its usage.
package net.javaguides.math;

import java.math.MathContext;
import java.math.RoundingMode;

/**
 * Class demonstrates the usage of MathContext class methods with examples
 * 
 * @author Ramesh Fadatare
 *
 */
public class MathContextMethodsExample {
    public static void main(String[] args) {
        MathContextMethodsExample example = new MathContextMethodsExample();

        example.toStringMethod();
        example.hashCodeMethod();
        example.getRoundingModeMethod();
        example.getPrecisionMethod();
        example.equalsMethod();
    }

    public void toStringMethod() {
        // create 2 MathContext objects
        MathContext mc1, mc2;

        // assign context settings to mc1, mc2
        mc1 = new MathContext(6, RoundingMode.DOWN);
        mc2 = new MathContext(20, RoundingMode.FLOOR);

        // create 2 String objects
        String s1, s2;

        // assign string representation of mc1, mc2 to s1, s2
        s1 = mc1.toString();
        s2 = mc2.toString();

        String str1 = "String representation of mc1 is " + s1;
        String str2 = "String representation of mc2 is " + s2;

        // print s1, s2 values
        System.out.println(str1);
        System.out.println(str2);
    }

    public void hashCodeMethod() {
        // create 2 MathContext objects
        MathContext mc1, mc2;

        // assign context settings to mc1, mc2
        mc1 = new MathContext(5);
        mc2 = new MathContext(20, RoundingMode.UNNECESSARY);

        // create 2 int objects
        int i1, i2;

        // assign hashcode of mc1, mc2 to i1, i2
        i1 = mc1.hashCode();
        i2 = mc2.hashCode();

        String str1 = "Hashcode of mc1 is " + i1;
        String str2 = "Hashcode of mc2 is " + i2;

        // print i1, i2 values
        System.out.println(str1);
        System.out.println(str2);
    }

    public void getRoundingModeMethod() {

        // create 2 MathContext objects
        MathContext mc1, mc2;

        // assign context settings to mc1, mc2
        mc1 = new MathContext(4);
        mc2 = new MathContext(50, RoundingMode.CEILING);

        // create 2 RoundingMode objects
        RoundingMode rm1, rm2;

        // assign roundingmode of mc1, mc2 to rm1, rm2
        rm1 = mc1.getRoundingMode();
        rm2 = mc2.getRoundingMode();

        String str1 = "Rounding Mode of mc1 is " + rm1;
        String str2 = "Rounding Mode of mc2 is " + rm2;

        // print rm1, rm2 values
        System.out.println(str1);
        System.out.println(str2);

    }

    public void getPrecisionMethod() {

        // create 2 MathContext objects
        MathContext mc1, mc2;

        // assign context settings to mc1, mc2
        mc1 = new MathContext(4, RoundingMode.CEILING);
        mc2 = new MathContext(50, RoundingMode.HALF_EVEN);

        // create 2 int objects
        int i1, i2;

        // assign precision of mc1, mc2 to i1, i2
        i1 = mc1.getPrecision();
        i2 = mc2.getPrecision();

        String str1 = "Precision of mc1 is " + i1;
        String str2 = "Precision of mc2 is " + i2;

        // print i1, i2 values
        System.out.println(str1);
        System.out.println(str2);
    }

    public void equalsMethod() {

        // create 3 MathContext objects
        MathContext mc1, mc2, mc3;

        // assign context settings to mc1, mc2, mc3
        mc1 = new MathContext(2);
        mc2 = new MathContext(2, RoundingMode.HALF_UP);
        mc3 = new MathContext(2, RoundingMode.HALF_DOWN);

        // create 2 boolean objects
        Boolean b1, b2;

        // compare context settings of mc1 with mc2, mc3
        b1 = mc1.equals(mc2);
        b2 = mc1.equals(mc3);

        String str1 = "Context settings of mc1 and mc2 are equal is " + b1;
        String str2 = "Context settings of mc1 and mc3 are equal is " + b2;

        // print b1, b2 values
        System.out.println(str1);
        System.out.println(str2);
    }
}
Output:
String representation of mc1 is precision=6 roundingMode=DOWN
String representation of mc2 is precision=20 roundingMode=FLOOR
Hashcode of mc1 is -1155810597
Hashcode of mc2 is 42723657
Rounding Mode of mc1 is HALF_UP
Rounding Mode of mc2 is CEILING
Precision of mc1 is 4
Precision of mc2 is 50
Context settings of mc1 and mc2 are equal is true
Context settings of mc1 and mc3 are equal is false
Below are similar articles you may like:

Comments