Java MathContext Class | getRoundingMode() Method with Example

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

MathContext Class getRoundingMode() method

  • getRoundingMode() method is available in java.math package.
  • getRoundingMode() method is used to get the RoundingMode value of this MathContext if defined and there are many constants of Rounding Mode like DOWN, CEILING, UNNECESSARY, FLOOR, etc.
  • getRoundingMode() 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.
  • getRoundingMode() method does not throw an exception at the time of getting round mode.

Syntax:

    public RoundingMode getRoundingMode();

Parameter(s):

  • None

Return value:

The return type of this method is RoundingMode, it returns the RoundingMode constants whatever be defined with this MathContext.

Example:

// Java program to demonstrate the example 
// of RoundingMode getRoundingMode() method of MathContext

import java.math.*;

public class GetRoundingModeOfMC {
    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);

        // returns the rounding mode of this MathContext
        // ma_co1 i.e. RoundingMode is the second
        // parameter set in MathContext Constructor
        // i.e. CEILING in ma_co1
        RoundingMode rm = ma_co1.getRoundingMode();
        System.out.println("ma_co1.getRoundingMode(): " + rm);

        // returns the rounding mode of this MathContext
        // ma_co2 i.e. RoundingMode is the second
        // parameter set in MathContext Constructor
        // i.e. HALF_UP in ma_co2 it the default set
        // when we don't use other rounding mode
        rm = ma_co2.getRoundingMode();
        System.out.println("ma_co2.getRoundingMode(): " + rm);

        // returns the rounding mode of this MathContext
        // ma_co3 i.e. RoundingMode is the second
        // parameter set in MathContext Constructor
        // i.e. FLOOR in ma_co1
        rm = ma_co3.getRoundingMode();
        System.out.println("ma_co3.getRoundingMode(): " + rm);
    }
}

Output

ma_co1.getRoundingMode(): CEILING
ma_co2.getRoundingMode(): HALF_UP
ma_co3.getRoundingMode(): FLOOR



Comments and Discussions!

Load comments ↻






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