Java教程

Java Math floor()

Java Math floor()

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

示例

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

Math.floor() 语法

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

Math.floor() 参数

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

Math.floor() 返回值

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

示例: Java Math.floor()

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

推荐教程

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