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


Java Java.lang.StrictMath.rint()用法及代碼示例


描述

這個java.lang.StrictMath.rint() 方法返回值最接近參數且等於數學整數的雙精度值。如果作為數學整數的兩個 double 值與參數的值同樣接近,則結果是偶數的整數值。它包括這些情況

  • 如果參數值已經等於一個數學整數,則結果與參數相同。
  • 如果自變量是NaN或無窮大或正零或負零,則結果與自變量相同。

聲明

以下是聲明java.lang.StrictMath.rint()方法

public static double rint(double a)

參數

a─ 這是要使用的值。

返回值

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

異常

NA

示例

下麵的例子展示了 java.lang.StrictMath.rint() 方法的用法。

package com.tutorialspoint;

import java.lang.*;

public class StrictMathDemo {

   public static void main(String[] args) {

      double d1 = 6.1 , d2 = 57.5 , d3 = 9.7;
   
      /* returns the double value that is close to the argument 
         and is equal to a mathematical integer. */

      double rintValue = StrictMath.rint(d1); 
      System.out.println("integer value closest to " + d1 + " = " + rintValue);

      rintValue = StrictMath.rint(d2); 
      System.out.println("integer value closest to " + d2 + " = " + rintValue);

      rintValue = StrictMath.rint(d3); 
      System.out.println("integer value closest to " + d3 + " = " + rintValue);
   }
}

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

integer value closest to 6.1 = 6.0
integer value closest to 57.5 = 58.0
integer value closest to 9.7 = 10.0

相關用法


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