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


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


描述

這個java.math.MathContext.getRoundingMode()返回舍入模式設置。

這將是 RoundingMode.CEILING、RoundingMode.DOWN、RoundingMode.FLOOR、RoundingMode.HALF_DOWN、RoundingMode.HALF_EVEN、RoundingMode.HALF_UP、RoundingMode.UNNECESSARY 或 RoundingMode.UP 之一。

聲明

以下是聲明java.math.MathContext.getRoundingMode()方法。

public RoundingMode getRoundingMode()

參數

NA

返回值

此方法返回一個 RoundingMode 對象,它是 roundingMode 設置的值。

異常

NA

示例

下麵的例子展示了 math.MathContext.getRoundingMode() 方法的用法。

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

讓我們編譯並運行上麵的程序,這將產生以下結果——

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

相關用法


注:本文由純淨天空篩選整理自 Java.math.MathContext.getRoundingMode() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。