MathContext MathContext(String) in Java

MathContext(String): This method is available in the java.math.MathContext class of Java.

Syntax:

java.math.MathContext.MathContext(String val)

This method takes one argument. This method constructs a new MathContext from a string. The string must be in the same format as that produced by the toString method.

An IllegalArgumentException is thrown if the precision section of the string is out of range (< 0) or the string is not in the format created by the toString method.

Parameters: One parameter is required for this method.

val: The string to be parsed.

Throws:

1. IllegalArgumentException - if the precision section is out of range or of incorrect format.

2. NullPointerException - if the argument is null

Approach 1: When no exception

Java

package com.MathContext;

import java.math.MathContext;

public class MathContext2 {
    public static void main(String[] args) {

        String val = "precision=5 roundingMode=HALF_UP";
        MathContext mathContext = new MathContext(val);

        System.out.println(mathContext);
    }
}

Output:

precision=5 roundingMode=HALF_UP


Approach 2: IllegalArgumentException

Java

package com.MathContext;

import java.math.MathContext;

public class MathContext2 {
    public static void main(String[] args) {

        String val = "118818";
        MathContext mathContext = new MathContext(val);

        System.out.println(mathContext);
    }
}

Output:

Exception in thread "main" java.lang.IllegalArgumentException: bad string format at java.base/java.math.MathContext.<init>(MathContext.java:200) at com.MathContext.MathContext2.main(MathContext2.java:9)


Approach 3: NullPointerException 

Java

package com.MathContext;

import java.math.MathContext;

public class MathContext2 {
    public static void main(String[] args) {

        String val = null;
        MathContext mathContext = new MathContext(val);

        System.out.println(mathContext);
    }
}

Output:

Exception in thread "main" java.lang.NullPointerException: null String at java.base/java.math.MathContext.<init>(MathContext.java:187) at com.MathContext.MathContext2.main(MathContext2.java:9)


Some other methods of MathContext class

MathContext.DECIMAL128A MathContext object with a precision setting matching the IEEE 754R Decimal128 format, 34 digits, and rounding mode of HALF_EVEN, the IEEE 754R default.

MathContext.DECIMAL32A MathContext object with a precision setting matching the IEEE 754R Decimal32 format, 7 digits, and a rounding mode of HALF_EVEN, the IEEE 754R default.

MathContext.DECIMAL64A MathContext object with a precision setting matching the IEEE 754R Decimal64 format, 16 digits, and a rounding mode of HALF_EVEN, the IEEE 754R default.

equals(Object)This method compares this MathContext with the specified Object for equality.

getPrecision()This method returns the precision setting. This value is always non-negative.

getRoundingMode()This method returns the roundingMode setting.

hashCode()This method returns the hash code for this MathContext.

toString()This method returns the string representation of this MathContext.

MathContext.UNLIMITEDA MathContext object whose settings have the values required for unlimited precision arithmetic.

MathContext(int)This method constructs a new MathContext with the specified precision and the HALF_UP rounding mode.

MathContext(String)This method constructs a new MathContext from a string. The string must be in the same format as that produced by the toString method.

MathContext(int, RoundingMode)This method constructs a new MathContext with a specified precision and rounding mode.

No comments:

Post a Comment