MathContext toString() Method in Java
The java.math.MathContext.toString() function is an in-built function in Java which returns the string representation of a MathContext object’s context settings.
The string returned represents the settings of the MathContext object as two space-separated words. These two space-separated strings are as follows :
- The first part of the returned string displays the value of the precision settings preceded by string “precision=”
- The second part of the returned string displays the value of the RoundingMode settings preceded by string “roundingMode=”
Syntax :
public String toString()
Parameter: The method accepts no parameter.
Return Values: This method returns a string representing the context settings of the object of the MathContext class in the format mentioned above.
Examples:
Input : m1 = new MathContext(2, RoundingMode.HALF_DOWN) Output : precision=2 roundingMode=HALF_DOWN Input : m1 = new MathContext(60) Output : precision=60 roundingMode=HALF_UP
Below programs illustrates the use of java.math.MathContext.toString() function :
Program 1:
// Java program to demonstrate toString() method import java.math.*; import java.io.*; class GFG { public static void main(String[] args) { // creating a MathContext object MathContext m1; // assign context settings to m1 m1 = new MathContext(37, RoundingMode.HALF_DOWN); System.out.println(m1.toString()); } } |
precision=37 roundingMode=HALF_DOWN
Program 2:
// Java program to demonstrate toString() method import java.math.*; import java.io.*; class GFG { public static void main(String[] args) { // Creating a MathContext object MathContext m1; // Assign context settings to m1 m1 = new MathContext(60); System.out.println(m1.toString()); } } |
precision=60 roundingMode=HALF_UP
Reference : https://docs.oracle.com/javase/7/docs/api/java/math/MathContext.html#toString()
Recommended Posts:
- MathContext equals() Method in Java
- MathContext getPrecision() Method in Java
- Java.lang.Short toString() method in Java with Examples
- HashSet toString() method in Java with Example
- LinkedHashSet toString() method in Java with Example
- AbstractSequentialList toString() method in Java with Example
- ArrayBlockingQueue toString() Method in Java
- CopyOnWriteArrayList toString() method in Java
- Stack toString() method in Java with Example
- Hashtable toString() Method in Java
- AbstractSet toString() method in Java with Example
- Vector toString() method in Java with Example
- StringJoiner toString() method in Java
- LinkedBlockingDeque toString() method in Java
- TreeSet toString() method in Java with Example
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



