Java Floor 函数是 Math 库中提供的数学函数(Mathematical Functions)之一。此数学 floor 函数返回小于或等于指定表达式或值且等于数学整数的最接近的双精度值。
Java floor 函数语法
Java 编程语言中 Math.floor 的基本语法如下所示。下面的函数将接受正数或负数双精度值作为参数。它返回小于或等于指定表达式或值且等于数学整数的最接近的双精度值。
static double floor(double number); //Return Type is Double // In order to use in program: Math.floor(double number);
Number:它可以是一个双精度值,也可以是一个数值表达式,您想在其上查找 floor 值。
- 如果 number 参数是正数或负数,Math 函数将返回 floor 值。
- 如果 number 参数是正零或负零,它将返回相同的参数。
- 如果 number 参数是 NaN (非数字),它将返回相同的参数。
Java math Floor 函数示例
floor 函数返回小于或等于给定数值的最近整数值。以下查询将展示使用 math.floor 的多种方法。
// Example
package MathFunctions;
public class FloorMethod {
public static void main(String[] args) {
double a = Math.floor(10.9666 - 14.9865 + 154.9852);
System.out.println("Floor value of Positive Number: " + Math.floor(10.25));
System.out.println("Floor value of Positive Number: " + Math.floor(10.95));
System.out.println("\nFloor value of Negative Number: " + Math.floor(-20.85));
System.out.println("Floor value of Negative Number: " + Math.floor(-20.25));
System.out.println("\nFloor value = " + a);
}
}

首先,我们声明了一个 Double 类型的变量,并直接对表达式执行了 Math.floor 函数。
double a = Math.floor(10.9666 - 14.9865 + 154.9852);
Math.floor(10.9666 – 14.9865 + 154.9852) ==> 150.9653 ==> 150
接下来,我们直接对正双精度值使用了该函数。如果您观察到下面的代码片段,尽管 10.95 接近 11,但 math.floor 返回了 10。这是因为 11 将大于给定的参数。
System.out.println("Floor value of Positive Number: " + Math.floor(10.25));
System.out.println("Floor value of Positive Number: " + Math.floor(10.95));
接下来,我们直接对负双精度值使用了Math 函数。
System.out.println("\nFloor value of Negative Number: " + Math.floor(-20.85));
System.out.println("Floor value of Negative Number: " + Math.floor(-20.25));
Java math floor on Array 示例
在这个数学程序中,我们将展示如何查找批量数据的 floor 值。在这里,我们将声明一个双精度类型的数组,并查找数组元素的最接近值。
package MathFunctions;
public class FloorMethodOnArray {
public static void main(String[] args) {
double [] myArray = {-15.69, 45.98, 44.21, -15.9999, -6.879, 3.4897};
for (int i = 0; i < myArray.length; i++) {
System.out.format("%.2f\n", Math.floor(myArray[i]));
}
}
}
-16.00
45.00
44.00
-16.00
-7.00
3.00
我们使用了For Loop来迭代数组。在 For Loop 中,我们将 i 值初始化为 0。接下来,编译器将检查条件(i < myArray.length)。
提示:myArray.length 查找 Java 数组的长度。
for (int i = 0; i < myArray.length; i++) {
以下语句在 System.out.format 语句中直接使用了 Math.floor 函数。在这里,它将调用静态的 double floor(double number) 方法来查找相应的最近(floor)值并打印输出。
System.out.format("%.2f\n", Math.floor(myArray[i]));
注意:要查找单个项目的最近值,请使用:Math.floor(myArray[index_position])
Java math floor on Arraylist 示例
在这个Java程序中,我们将声明一个双精度类型的 ArrayList,并查找列表元素的最接近值。
package MathFunctions;
import java.util.ArrayList;
public class FloorMethodOnList {
public static void main(String[] args) {
ArrayList<Double> myList = new ArrayList<Double>(5);
myList.add(-10.789);
myList.add(-10.65);
myList.add(15.9876);
myList.add(40.9958979);
myList.add(-44.4589);
for (double x : myList) {
System.out.println(Math.floor(x));
}
}
}
-11.0
-11.0
15.0
40.0
-45.0
在这个数学 floor 函数示例中,我们使用了 For Loop 来迭代 ArrayList 中的双精度值。
for (double x : myList) {
我们直接在 System.out.format 语句中使用了它。在这里,编译器将调用 math 方法(static double floor(double x))来查找相应的最近值并打印输出。
System.out.println(Math.floor(x));