Java教程

Java Math ceil()

Java Math ceil()

在本教程中,我们将通过示例了解 Java Math.ceil() 方法。
ceil() 方法将指定的双精度值向上舍入并返回。舍入后的值将等于Math整数。也就是说,值 3.24 将四舍五入为 4.0,等于整数 4。

示例

class Main {
  public static void main(String[] args) {
      double a = 3.24; System.out.println(Math.ceil(a)); 
  }
}
// Output: 4.0

Math.ceil() 的语法

ceil() 方法的语法是:
Math.ceil(double value)
这里, ceil() 是一个静态方法。因此,我们使用类名访问该方法, Math

ceil() 参数

ceil() 方法接受一个参数。
value-要向上舍入的数字

ceil() 返回值

返回等于Math整数的舍入值
注意: 返回值将是大于或等于指定参数的最小值。

示例: Java Math.ceil()

class Main {
  public static void main(String[] args) {
    // Math.ceil() method
    // value greater than 5 after decimal
     double a = 1.878;
    System.out.println(Math.ceil(a)); // 2.0 
    // value equals to 5 after decimal
     double b = 1.5;
    System.out.println(Math.ceil(b)); // 2.0 
    // value less than 5 after decimal
     double c = 1.34;
    System.out.println(Math.ceil(c)); // 2.0 
  }
}

推荐教程

Math.floor() ​​Math.round()
昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4