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


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


java.lang.StrictMath類的所有方法:設置1,設置2

java.lang.StrictMath.ceil()是Java中的內置方法,用於返回最小的double值,該值大於或等於給定的double參數且等於整數。它產生三個特殊結果:

  • 當給定參數等於整數時,結果與參數相同。
  • 當給定參數為NaN,無窮大,正零或負零時,結果與參數相同。
  • 當給定參數小於0且大於-1.0時,結果為負0。

用法:


public static double ceil(double num)

參數:該方法接受一個要返回其上限值的double類型的參數num。

返回值:該方法返回最小的浮點值,該值最接近負無窮大,並且大於或等於給定參數,並且還等於整數。

例子:

Input: num = 2.7
Output: 3.0

Input: num = -8.7
Output: -8.0

下麵的程序演示了java.lang.StrictMath.ceil()方法:
示例1:

// Java praogram to illustrate the 
// java.lang.StrictMath.ceil() 
import java.lang.*; 
  
public class Geeks { 
  
    public static void main(String[] args) 
    { 
  
        double num1 = 8.7, num2 = 7.1, num3 = 3.5; 
  
        // It returns ceiling value 
        double cValue = StrictMath.ceil(num1); 
        System.out.println("The Ceil value of "+ 
                             num1+" = " + cValue); 
  
        cValue = StrictMath.ceil(num2); 
        System.out.println("The Ceil value of "+ 
                             num2+" = " + cValue); 
  
        cValue = StrictMath.ceil(num3); 
        System.out.println("The Ceil value of "+ 
                             num3+" = " + cValue); 
    } 
}
輸出:
The Ceil value of 8.7 = 9.0
The Ceil value of 7.1 = 8.0
The Ceil value of 3.5 = 4.0

示例2:

// Java praogram to illustrate the 
// java.lang.StrictMath.ceil() 
import java.lang.*; 
  
public class Geeks { 
  
    public static void main(String[] args) 
    { 
  
        double num1 = -8.7, num2 = -7.1, num3 = -3.5; 
  
        // It returns ceiling value 
        double cValue = StrictMath.ceil(num1); 
        System.out.println("The Ceil value of "+ 
                             num1+" = " + cValue); 
  
        cValue = StrictMath.ceil(num2); 
        System.out.println("The Ceil value of "+ 
                             num2+" = " + cValue); 
  
        cValue = StrictMath.ceil(num3); 
        System.out.println("The Ceil value of "+ 
                             num3+" = " + cValue);  
   } 
}
輸出:
The Ceil value of -8.7 = -8.0
The Ceil value of -7.1 = -7.0
The Ceil value of -3.5 = -3.0


相關用法


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