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));
} } // Output: 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() 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
} }

另请阅读

你觉得这篇文章有帮助吗?

我们的高级学习平台,凭借十多年的经验和数千条反馈创建。

以前所未有的方式学习和提高您的编程技能。

试用 Programiz PRO
  • 交互式课程
  • 证书
  • AI 帮助
  • 2000+ 挑战