Java MathContext Class | toString() Method with Example

MathContext Class toString() method: Here, we are going to learn about the toString() method of MathContext Class with its syntax and example.
Submitted by Preeti Jain, on May 12, 2020

MathContext Class toString() method

  • toString() method is available in java.math package.
  • toString() method is used to represent this object (MathContext) as a String.
  • toString() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.
  • toString() method does not throw an exception at the time of representing a string.

Syntax:

    public String toString();

Parameter(s):

  • None

Return value:

The return type of this method is String, it retrieves String object that represents this MathContext object.

Example:

// Java program to demonstrate the example 
// of String toString() method of MathContext

import java.math.*;

public class ToStringOfMC {
    public static void main(String args[]) {
        // Initialize three MathContext objects  
        MathContext ma_co1 = new MathContext(3, RoundingMode.CEILING);
        MathContext ma_co2 = new MathContext(4);
        MathContext ma_co3 = new MathContext(6, RoundingMode.FLOOR);

        // represents this MathContext ma_co1
        // as a String
        String str_rep = ma_co1.toString();
        System.out.println("ma_co1.toString(): " + str_rep);

        // represents this MathContext ma_co2
        // as a String
        str_rep = ma_co2.toString();
        System.out.println("ma_co2.toString(): " + str_rep);

        // represents this MathContext ma_co3
        // as a String
        str_rep = ma_co3.toString();
        System.out.println("ma_co3.toString(): " + str_rep);
    }
}

Output

ma_co1.toString(): precision=3 roundingMode=CEILING
ma_co2.toString(): precision=4 roundingMode=HALF_UP
ma_co3.toString(): precision=6 roundingMode=FLOOR


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.