Java.math.MathContext.getRoundingMode() Method

Advertisements


Description

The java.math.MathContext.getRoundingMode() returns the roundingMode setting.

This will be one of RoundingMode.CEILING, RoundingMode.DOWN, RoundingMode.FLOOR, RoundingMode.HALF_DOWN, RoundingMode.HALF_EVEN, RoundingMode.HALF_UP, RoundingMode.UNNECESSARY, or RoundingMode.UP.

Declaration

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

public RoundingMode getRoundingMode()

Parameters

  • NA

Return Value

This method returns a RoundingMode object which is the value of the roundingMode setting.

Exception

  • NA

Example

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

        // create 2 RoundingMode objects
        RoundingMode rm1, rm2;

        // assign roundingmode of mc1, mc2 to rm1, rm2
        rm1 = mc1.getRoundingMode();
	rm2 = mc2.getRoundingMode();

        String str1 = "Rounding Mode of mc1 is " + rm1;
	String str2 = "Rounding Mode of mc2 is " + rm2;

	// print rm1, rm2 values
        System.out.println( str1 );
	System.out.println( str2 );
    }
}

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

Rounding Mode of mc1 is HALF_UP
Rounding Mode of mc2 is CEILING



Advertisements
Advertisements