Java.math.MathContext.getPrecision() Method


Advertisements


Description

The java.math.MathContext.getPrecision() returns the precision setting. This value is always non-negative.

Declaration

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

public int getPrecision()

Parameters

NA

Return Value

This method returns an int which is the value of the precision setting.

Exception

NA

Example

The following example shows the usage of math.MathContext.getPrecision() 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(4, RoundingMode.CEILING);
      mc2 = new MathContext(50, RoundingMode.HALF_EVEN);

      // create 2 int objects
      int i1, i2;

      // assign precision of mc1, mc2 to i1, i2
      i1 = mc1.getPrecision();
      i2 = mc2.getPrecision();

      String str1 = "Precision of mc1 is " + i1;
      String str2 = "Precision of mc2 is " + i2;

      // print i1, i2 values
      System.out.println( str1 );
      System.out.println( str2 );
   }
}

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

Precision of mc1 is 4
Precision of mc2 is 50

java_math_mathcontext.htm

Advertisements