Java - Math rint(double x) 方法



描述

Java Math rint(double a) 方法返回與引數最接近且等於數學整數的雙精度數值。如果兩個雙精度數值都與數學整數同樣接近,則結果為偶數的整數數值。特殊情況 -

  • 如果引數值已經等於數學整數,則結果與引數相同。

  • 如果引數為 NaN 或無窮大或正零或負零,則結果與引數相同。

宣告

以下是 java.lang.Math.rint() 方法的宣告

public static double rint(double a)

引數

a − 雙精度數值。

返回值

此方法返回與 a 最接近的浮點數值,該數值等於數學整數。

異常

示例:獲取正雙精度數值的最接近整數

以下示例演示瞭如何使用 Math rint() 方法獲取正雙精度數值的雙精度數值。

package com.tutorialspoint;
public class MathDemo {
   public static void main(String[] args) {

      // get a double number
      double x = 1654.9874;

      // find the closest integers for this double number
      System.out.println("Math.rint(" + x + ")=" + Math.rint(x));
   }
}

輸出

讓我們編譯並執行以上程式,這將產生以下結果 -

Math.rint(1654.9874)=1655.0

示例:獲取負雙精度數值的最接近整數

以下示例演示瞭如何使用 Math rint() 方法獲取負雙精度數值的雙精度數值。

package com.tutorialspoint;
public class MathDemo {
   public static void main(String[] args) {

      // get a double number
      double x = -9765.134;

      // find the closest integers for this double number
      System.out.println("Math.rint(" + x + ")=" + Math.rint(x));
   }
}

輸出

讓我們編譯並執行以上程式,這將產生以下結果 -

Math.rint(-9765.134)=-9765.0

示例:獲取零雙精度數值的最接近整數

以下示例演示瞭如何使用 Math rint() 方法獲取零雙精度數值的雙精度數值。

package com.tutorialspoint;
public class MathDemo {
   public static void main(String[] args) {

      // get a double number
      double x = -0.0;
      double y = 0.0;	  

      // find the closest integers for these double number
      System.out.println("Math.rint(" + x + ")=" + Math.rint(x));
	  System.out.println("Math.rint(" + y + ")=" + Math.rint(y));
   }
}

輸出

讓我們編譯並執行以上程式,這將產生以下結果 -

Math.rint(-0.0)=-0.0
Math.rint(0.0)=0.0
java_lang_math.htm
廣告