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


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


rint()是Java中StrictMath類的內置方法,用於獲取值最接近參數且等於整數的double值。它返回一個整數值,當作為整數的兩個double值均相等地接近給定參數的值時,它甚至是整數。當參數值已經等於整數時,它返回與參數相同的值;當參數為NaN或無窮大或正零或負零時,它返回與參數相同的值。

用法:

public static double rint(double num)

參數:該方法接受要使用此方法轉換的雙精度類型的單個參數num。


返回值:該方法返回等於整數的最接近的浮點值。

例子:

Input: num =72.2
Output: 72.0

以下示例程序旨在說明rint()方法:

示例1:

// Java praogram to illustrate the 
// java.lang.StrictMath.rint() 
  
import java.lang.*; 
  
public class Geeks { 
    public static void main(String[] args) 
    { 
  
        // Get a double number 
        double num1 = 87.1; 
  
        // Convert the double number using rint() method 
        double rint_Value = StrictMath.rint(num1); 
  
        // Print the result 
        System.out.println(" The Integer value closest to "
                         + num1 + " = " + rint_Value); 
  
        // Get a double number 
        double num2 = 65.9; 
  
        // Convert the double number using rint() method 
        rint_Value = StrictMath.rint(num1); 
  
        // Print the result 
        System.out.println(" The Integer value closest to "
                         + num1 + " = " + rint_Value); 
    } 
}
輸出:
The Integer value closest to 87.1 = 87.0 
The Integer value closest to 87.1 = 87.0

示例2:

// Java praogram to illustrate the 
// java.lang.StrictMath.rint() 
  
import java.lang.*; 
  
public class Geeks { 
  
    public static void main(String[] args) 
    { 
  
        // Get a double number 
        double num1 = -65.5; 
  
        // Convert the double number using rint() method 
        double rint_Value = StrictMath.rint(num1); 
  
        // Print the result 
        System.out.println(" The Integer value closest to "
                         + num1 + " = " + rint_Value); 
  
        // Get a double number 
        double num2 = -42.7; 
  
        // Convert the double number using rint() method 
        rint_Value = StrictMath.rint(num1); 
  
        // Print the result 
        System.out.println(" The Integer value closest to "
                         + num1 + " = " + rint_Value); 
    } 
}
輸出:
The Integer value closest to -65.5 = -66.0 
The Integer value closest to -65.5 = -66.0


相關用法


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