當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Java MathContext getRoundingMode()用法及代碼示例

MathContext類getRoundingMode()方法

  • getRoundingMode() 方法可在java.math包。
  • getRoundingMode() 方法用於獲取此 MathContext 的 RoundingMode 值(如果已定義)並且有許多舍入模式常量,如 DOWN、CEILING、UNNECESSARY、FLOOR 等。
  • getRoundingMode() 方法是一個非靜態方法,它隻能通過類對象訪問,如果我們嘗試使用類名訪問方法,那麽我們將得到一個錯誤。
  • getRoundingMode() 方法在獲取輪次模式時不會拋出異常。

用法:

    public RoundingMode getRoundingMode();

參數:

  • None

返回值:

這個方法的返回類型是RoundingMode,它返回任何用此 MathContext 定義的 RoundingMode 常量。

例:

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

輸出

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


相關用法


注:本文由純淨天空篩選整理自Preeti Jain大神的英文原創作品 Java MathContext Class | getRoundingMode() Method with Example。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。