Java.math.MathContext.toString() Method



Description

The java.math.MathContext.toString() returns the string representation of this MathContext.

The String returned represents the settings of the MathContext object as two space-delimited words (separated by a single space character, '\u0020', and with no leading or trailing white space), as follows −

  • The string "precision = ", immediately followed by the value of the precision setting as a numeric string as if generated by the Integer.toString method.

  • The string "roundingMode = ", immediately followed by the value of the roundingMode setting as a word. This word will be the same as the name of the corresponding public constant in the RoundingMode enum.

Additional words may be appended to the result of toString in the future if more properties are added to this class.

Declaration

Following is the declaration for java.math.MathContext.toString() method.

public String toString()

Overrides

toString in class Object.

Parameters

NA

Return Value

This method returns a String representing the context settings.

Exception

NA

Example

The following example shows the usage of math.MathContext.toString() method.

package com.tutorialspoint;

import java.math.*;

public class MathContextDemo {

   public static void main(String[] args) {

      // 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 );
   }
}

Let us compile and run the above program, this will produce the following result −

String representation of mc1 is precision = 6 roundingMode = DOWN
String representation of mc2 is precision = 20 roundingMode = FLOOR
java_math_mathcontext.htm
Advertisements