# Java Math.floor Method, floorDiv and floorMod

Use the Math.floor method to reduce numbers to the nearest lower integer. Review Math.floorDiv.
Math.floor. This function maps a number to the nearest lowest integer. For example 4.9 is mapped to 4. It works on negative numbers too: -3.1 maps to -4.Math
With related methods, floorDiv and floorMod we compute a division expression and then take the floor of that result. Div is division, and mod is modulo division.Modulo
Floor example. The floor is always beneath us. Likewise the Math.floor method always returns a lower integer. For negative numbers, floor() still lowers.

Here: We use floor on three double values. Floor returns an int but we can store this in a double if we want.

Integer, max

Println: We use System.out.println to display the double results to the console.

Println
Java program that uses Math.floor import java.lang.Math; public class Program { public static void main(String[] args) { // These are all reduced, even the negative number. double floor1 = Math.floor(1.9); double floor2 = Math.floor(1.1); double floor3 = Math.floor(-1.3); System.out.println(floor1); System.out.println(floor2); System.out.println(floor3); } } Output 1.0 1.0 -2.0
FloorDiv. 9 divided by 2 is 4.5, but with floorDiv we take the floor of that result. This returns 4. So floorDiv combines division with Math.floor.

Argument 1: This is the number being divided. In math class this number is called the numerator.

Argument 2: This is the number we are dividing the first number by. This is the divisor number.

Java program that uses Math.floorDiv public class Program { public static void main(String[] args) { // Use Math.floorDiv to compute the floor of a division. // ... The first argument is the number being divided. // ... The second argument is the divisor. int result = Math.floorDiv(9, 2); System.out.println(result); // This is the same division with no floor. double result2 = (double) 9 / 2; System.out.println(result2); } } Output 4 4.5
FloorMod. This method is the same as floorDiv except it uses modulo division. In most cases it is the same as a regular modulo division.

Difference: When one number is positive and the other is negative, Math.floorMod will have a different result from a modulo expression.

Java program that uses Math.floorMod public class Program { public static void main(String[] args) { // The remainder of 10 modulo 6 is 4. int result = Math.floorMod(10, 6); int result2 = 10 % 6; System.out.println(result); System.out.println(result2); // Use negative numbers mixed with positive numbers. // ... These are different with floorMod. int result3 = Math.floorMod(10, -6); int result4 = 10 % -6; System.out.println(result3); System.out.println(result4); } } Output 4 4 -2 4
A review. The floor functions in Java are present alongside ceiling functions. We map numbers to the nearest lower (for floor) or higher (for ceiling) integers.Math.ceil
© 2007-2020 Sam Allen. Every person is special and unique. Send bug reports to info@dotnetperls.com.
Home
Dot Net Perls