Java Math.floor() method
DevSniper
169 Views
public static double floor(double a)
Java Math.floor() is a in-build static method provided by Math class to perform a specific mathematical operation. It is used to find the largest double value that is less than or equal to the argument and is equal to a mathematical integer.
- If the parameter is a double value, either positive or negative, this method will return floor value.
- If the parameter is NaN this method will return NaN. If you want to understand NaN in Java click me for NaN in Java
- If the parameter is positive Infinity, this method will return +Infinity. To understand +Infinity click me for +Infinity in Java
- If the parameter is negative Infinity, this method will return -Infinity. To understand -Infinity chick me for -Infinity in Java
Java Math.floor with positive value
public class Program { public static void main(String []args) { System.out.println("Math.floor(10) = "+ Math.floor(10)); System.out.println("Math.floor(10L) = "+ Math.floor(10L)); System.out.println("Math.floor(10f) = "+ Math.floor(10f)); System.out.println("Math.floor(10.0) = "+ Math.floor(10.0)); System.out.println("Math.floor(9.7f) = "+ Math.floor(9.7f)); System.out.println("Math.floor(9.5f) = "+ Math.floor(9.5f)); System.out.println("Math.floor(9.1f) = "+ Math.floor(9.1f)); System.out.println("Math.floor(9.7) = "+ Math.floor(9.7)); System.out.println("Math.floor(9.5) = "+ Math.floor(9.5)); System.out.println("Math.floor(9.1) = "+ Math.floor(9.1)); } }
Math.floor(10) = 10.0 Math.floor(10L) = 10.0 Math.floor(10f) = 10.0 Math.floor(10.0) = 10.0 Math.floor(9.7f) = 9.0 Math.floor(9.5f) = 9.0 Math.floor(9.1f) = 9.0 Math.floor(9.7) = 9.0 Math.floor(9.5) = 9.0 Math.floor(9.1) = 9.0
Java Math.floor with negative value
public class Program { public static void main(String []args) { System.out.println("Math.floor(-10) = "+ Math.floor(-10)); System.out.println("Math.floor(-10L) = "+ Math.floor(-10L)); System.out.println("Math.floor(-10f) = "+ Math.floor(-10f)); System.out.println("Math.floor(-10.0) = "+ Math.floor(-10.0)); System.out.println("Math.floor(-9.7f) = "+ Math.floor(-9.7f)); System.out.println("Math.floor(-9.5f) = "+ Math.floor(-9.5f)); System.out.println("Math.floor(-9.1f) = "+ Math.floor(-9.1f)); System.out.println("Math.floor(-9.7) = "+ Math.floor(-9.7)); System.out.println("Math.floor(-9.5) = "+ Math.floor(-9.5)); System.out.println("Math.floor(-9.1) = "+ Math.floor(-9.1)); } }
Math.floor(-10) = -10.0 Math.floor(-10L) = -10.0 Math.floor(-10f) = -10.0 Math.floor(-10.0) = -10.0 Math.floor(-9.7f) = -10.0 Math.floor(-9.5f) = -10.0 Math.floor(-9.1f) = -10.0 Math.floor(-9.7) = -10.0 Math.floor(-9.5) = -10.0 Math.floor(-9.1) = -10.0
Java Math.floor with positive Infinity
public class Program { public static void main(String []args) { System.out.println("Math.floor(10/0.0) = "+ Math.floor(10/0.0) ); System.out.println("Math.floor(10L/0.0) = "+ Math.floor(10L/0.0) ); System.out.println("Math.floor(10f/0.0) = "+ Math.floor(10/0.0) ); System.out.println("Math.floor(10.1/0.0) = "+ Math.floor(10.1/0.0) ); System.out.println("Math.floor(10f/0) = "+ Math.floor(10f/0) ); System.out.println("Math.floor(10f/-0) = "+ Math.floor(10f/-0) ); System.out.println("Math.floor(-10/-0f) = "+ Math.floor(-10/-0f) ); System.out.println("Math.floor(10.1/0) = "+ Math.floor(10.1/0) ); System.out.println("Math.floor(10.1/-0) = "+ Math.floor(10.1/-0) ); System.out.println("Math.floor(-10.1/-0.0) = "+ Math.floor(-10/-0.0) ); } }
Math.floor(10/0.0) = Infinity Math.floor(10L/0.0) = Infinity Math.floor(10f/0.0) = Infinity Math.floor(10.1/0.0) = Infinity Math.floor(10f/0) = Infinity Math.floor(10f/-0) = Infinity Math.floor(-10/-0f) = Infinity Math.floor(10.1/0) = Infinity Math.floor(10.1/-0) = Infinity Math.floor(-10.1/-0.0) = Infinity
Java Math.floor with negative Infinity
public class Program { public static void main(String []args) { System.out.println("Math.floor(10/-0.0) = "+ Math.floor(10/-0.0) ); System.out.println("Math.floor(10L/-0.0) = "+ Math.floor(10L/-0.0) ); System.out.println("Math.floor(10f/-0.0) = "+ Math.floor(10f/-0.0) ); System.out.println("Math.floor(-10f/0.0) = "+ Math.floor(-10f/0.0) ); System.out.println("Math.floor(-10f/0) = "+ Math.floor(-10f/0) ); System.out.println("Math.floor(10.1/-0.0) = "+ Math.floor(10.1/-0.0) ); System.out.println("Math.floor(-10.1/0.0) = "+ Math.floor(-10.1/0.0) ); System.out.println("Math.floor(-10.1/0) = "+ Math.floor(-10.1/0) ); } }
Math.floor(10/-0.0) = -Infinity Math.floor(10L/-0.0) = -Infinity Math.floor(10f/-0.0) = -Infinity Math.floor(-10f/0.0) = -Infinity Math.floor(-10f/0) = -Infinity Math.floor(10.1/-0.0) = -Infinity Math.floor(-10.1/0.0) = -Infinity Math.floor(-10.1/0) = -Infinity
Java Math.floor with NaN
public class Program { public static void main(String []args) { System.out.println("Math.floor(0/0.0) = "+ Math.floor(0/0.0) ); System.out.println("Math.floor(0/-0.0) = "+ Math.floor(0/-0.0) ); System.out.println("Math.floor(-0/0.0) = "+ Math.floor(-0/0.0) ); System.out.println("Math.floor(-0/-0.0) = "+ Math.floor(-0/-0.0) ); System.out.println("Math.floor(0/0f) = "+ Math.floor(0/0f) ); System.out.println("Math.floor(0/-0f) = "+ Math.floor(0/-0f) ); System.out.println("Math.floor(-0/0f) = "+ Math.floor(-0/0f) ); System.out.println("Math.floor(-0/-0f) = "+ Math.floor(-0/-0f) ); } }
Math.floor(0/0.0) = NaN Math.floor(0/-0.0) = NaN Math.floor(-0/0.0) = NaN Math.floor(-0/-0.0) = NaN Math.floor(0/0f) = NaN Math.floor(0/-0f) = NaN Math.floor(-0/0f) = NaN Math.floor(-0/-0f) = NaN
- This method is helpful in scenarios where precise handling of numerical data is required such as in geometric calculations , financial calculations etc.
- This method is useful in any situation where precision in rounding is crucial.
- This method is useful to achieve accurate results in programming endeavors, ensuring robustness and correctness in mathematical operations.