带示例的 Java floor() 方法
java.lang.Math.floor() 返回小于或等于参数且等于数学整数的最大(最接近正无穷大)的 double 值。特殊情况 −
如果参数值本身已等于数学整数,则结果与参数相同。
如果参数是 NaN 或无穷大或正零或负零,那么结果与参数相同。
我们现在看一个示例来实现 Java 中的 floor() 方法 −
示例
import java.lang.*; public class Demo { public static void main(String[] args) { // get two double numbers double x = 60984.1; double y = -497.99; // call floor and print the result System.out.println("Math.floor(" + x + ")=" + Math.floor(x)); System.out.println("Math.floor(" + y + ")=" + Math.floor(y)); System.out.println("Math.floor(0)=" + Math.floor(0)); } }
输
Math.floor(60984.1)=60984.0 Math.floor(-497.99)=-498.0 Math.floor(0)=0.0
Learn Java in-depth with real-world projects through our Java certification course. Enroll and become a certified expert to boost your career.
示例
我们现在看另一个示例,其中我们将检查负值和其他值 −
import java.lang.*; public class Demo { public static void main(String[] args) { // get two double numbers double x = 0.0; double y = -5.7; double z = 1.0/0; // call floor and print the result System.out.println("Math.floor(" + x + ")=" + Math.floor(x)); System.out.println("Math.floor(" + y + ")=" + Math.floor(y)); System.out.println("Math.floor(" + z + ")=" + Math.floor(z)); } }
输
Math.floor(0.0)=0.0 Math.floor(-5.7)=-6.0 Math.floor(Infinity)=Infinity
广告