Java.math.MathContext.getRoundingMode()方法範例


 java.math.MathContext.getRoundingMode() 返回RoundingMode的設定。

這會是以下幾個之一的值:RoundingMode.CEILING, RoundingMode.DOWN, RoundingMode.FLOOR, RoundingMode.HALF_DOWN, RoundingMode.HALF_EVEN, RoundingMode.HALF_UP, RoundingMode.UNNECESSARY, or RoundingMode.UP.

宣告

以下是java.math.MathContext.getRoundingMode()方法的宣告

public RoundingMode getRoundingMode()

引數

  • NA

返回值

該方法返回一個RoundingMode物件,這是RoundingMode設定的值。

異常

  • NA

例子

下面的例子顯示math.MathContext.getRoundingMode()方法的用法

package com.yiibai;

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