跳到主要内容

Java Math floor() 方法

floor() 方法将指定的 double 值向下取整并返回。取整后的值将等于一个数学整数。也就是说,值 3.8 将被取整为 3.0,等于整数 3

示例

class Main {
public static void main(String[] args) {

double a = 3.8;
System.out.println(Math.floor(a));

}
}

// 输出: 3.0

Math.floor() 方法的语法

floor() 方法的语法是:

Math.floor(double value)

这里,floor() 是一个静态方法。因此,我们使用类名 Math 来访问该方法。

Math.floor() 方法的参数

floor() 方法接受单个参数。

  • value - 需要向上取整的数字

Math.floor() 方法的返回值

  • 返回等于数学整数的取整值

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

示例:Java Math.floor()

class Main {
public static void main(String[] args) {

// Math.floor() 方法
// 小数点后大于 5 的值
double a = 1.878;
System.out.println(Math.floor(a)); // 1.0

// 小数点后等于 5 的值
double b = 1.5;
System.out.println(Math.floor(b)); // 1.0

// 小数点后小于 5 的值
double c = 1.34;
System.out.println(Math.floor(c)); // 1.0

}
}

推荐教程