Creation and Initialization of MathContext objects

The MathContext class provides multiple ways to create and initialize objects that define the precision and rounding mode for decimal arithmetic using BigDecimal. The creation of a MathContext object is typically done through its constructors, where you specify the number of significant digits (precision) and optionally the rounding behavior. When you instantiate a MathContext, you’re essentially setting the rules for how numbers should be handled during calculations — for instance, whether results should be rounded up, down, or to the nearest value, and how many digits should be retained in the final output.

Java provides four overloaded constructors to create a MathContext instance. You can either specify only the precision (which uses a default rounding mode of HALF_UP), or you can provide both the precision and the desired rounding mode explicitly. There is also a constructor that takes a string representation, which is useful when reading configurations dynamically from a file or user input. Once a MathContext object is created, it remains immutable, meaning the precision and rounding mode cannot be changed later. This design promotes thread safety and ensures consistent behavior across different parts of the application.

Constructors

Methods

Example Program: Demonstrating MathContext Creation and Use

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

public class MathContextDemo {
    public static void main(String[] args) {
        // Constructor 1: Only precision, default rounding HALF_UP
        MathContext mc1 = new MathContext(4);
        
        // Constructor 2: Precision and custom rounding mode
        MathContext mc2 = new MathContext(3, RoundingMode.DOWN);
        
        // Constructor 3: Using string input
        MathContext mc3 = new MathContext("precision=5 roundingMode=CEILING");

        // Constructor 4: Copy constructor
        MathContext mc4 = new MathContext(mc2);

        // BigDecimal to be used
        BigDecimal value = new BigDecimal("123.456789");

        System.out.println("Original Value: " + value);
        System.out.println("Rounded with mc1 (precision=4, HALF_UP): " + value.round(mc1));
        System.out.println("Rounded with mc2 (precision=3, DOWN): " + value.round(mc2));
        System.out.println("Rounded with mc3 (precision=5, CEILING): " + value.round(mc3));
        System.out.println("Rounded with mc4 (copy of mc2): " + value.round(mc4));
    }
}
/*
Original Value: 123.456789
Rounded with mc1 (precision=4, HALF_UP): 123.5
Rounded with mc2 (precision=3, DOWN): 123
Rounded with mc3 (precision=5, CEILING): 123.46
Rounded with mc4 (copy of mc2): 123
*/

Program on Demonstrating MathContext Methods

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

public class MathContextMethodsDemo {

    public static void main(String[] args) {
        // Create two MathContext objects
        MathContext mc1 = new MathContext(4, RoundingMode.HALF_UP);
        MathContext mc2 = new MathContext(4, RoundingMode.HALF_UP);
        MathContext mc3 = new MathContext(6, RoundingMode.DOWN);

        // Display precision and rounding mode
        System.out.println("MathContext mc1 Precision: " + mc1.getPrecision());
        System.out.println("MathContext mc1 RoundingMode: " + mc1.getRoundingMode());

        // Compare two MathContext objects
        System.out.println("mc1.equals(mc2)? " + mc1.equals(mc2)); // true
        System.out.println("mc1.equals(mc3)? " + mc1.equals(mc3)); // false

        // Display hash codes
        System.out.println("mc1 HashCode: " + mc1.hashCode());
        System.out.println("mc2 HashCode: " + mc2.hashCode());
        System.out.println("mc3 HashCode: " + mc3.hashCode());

        // String representation
        System.out.println("mc1 toString(): " + mc1.toString());
        System.out.println("mc3 toString(): " + mc3.toString());

        // Using MathContext with BigDecimal
        BigDecimal value = new BigDecimal("12345.6789");

        BigDecimal result1 = value.round(mc1);
        BigDecimal result2 = value.round(mc3);

        System.out.println("Original Value: " + value);
        System.out.println("Rounded with mc1 (precision=4, HALF_UP): " + result1);
        System.out.println("Rounded with mc3 (precision=6, DOWN): " + result2);
    }
}
/*
MathContext mc1 Precision: 4
MathContext mc1 RoundingMode: HALF_UP
mc1.equals(mc2)? true
mc1.equals(mc3)? false
mc1 HashCode: [some number]
mc2 HashCode: [same as mc1]
mc3 HashCode: [different number]
mc1 toString(): precision=4 roundingMode=HALF_UP
mc3 toString(): precision=6 roundingMode=DOWN
Original Value: 12345.6789
Rounded with mc1 (precision=4, HALF_UP): 12350
Rounded with mc3 (precision=6, DOWN): 12345.6
*/
Scroll to Top