Java - StrictMath rint(double x) 方法



描述

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

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

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

宣告

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

public static double rint(double a)

引數

a - 雙精度數值。

返回值

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

異常

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

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

package com.tutorialspoint;
public class StrictMathDemo {
   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("StrictMath.rint(" + x + ")=" + StrictMath.rint(x));
   }
}

輸出

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

StrictMath.rint(1654.9874)=1655.0

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

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

package com.tutorialspoint;
public class StrictMathDemo {
   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("StrictMath.rint(" + x + ")=" + StrictMath.rint(x));
   }
}

輸出

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

StrictMath.rint(-9765.134)=-9765.0

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

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

package com.tutorialspoint;
public class StrictMathDemo {
   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("StrictMath.rint(" + x + ")=" + StrictMath.rint(x));
	  System.out.println("StrictMath.rint(" + y + ")=" + StrictMath.rint(y));
   }
}

輸出

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

StrictMath.rint(-0.0)=-0.0
StrictMath.rint(0.0)=0.0
java_lang_strictmath.htm
廣告

© . All rights reserved.