Close

Java - Math.floor() Examples

Java Java API 


Class:

java.lang.Math

java.lang.Objectjava.lang.Objectjava.lang.Mathjava.lang.MathLogicBig

Method:

public static double floor(double a)

Returns the largest (closest to positive infinity) double value that is less than or equal to the argument and is equal to a mathematical integer.


Examples


package com.logicbig.example.math;

public class FloorExample {


public static void main(String... args) {
findFloor(75.5);
findFloor(-3);
findFloor(0.91);
findFloor(-0.91);
}

private static void findFloor(double x) {
double floor_value = Math.floor(x);
System.out.printf(" Math.floor(%s) = %.2f%n", x, floor_value);
}
}

Output

 Math.floor(75.5) = 75.00
Math.floor(-3.0) = -3.00
Math.floor(0.91) = 0.00
Math.floor(-0.91) = -1.00




This example shows how to floor round a double to a given number of decimal places

package com.logicbig.example.math;

public class FloorExample2 {


public static void main(String... args) {
roundUp(115.9716123, 3);
roundUp(91.1236234, 2);
roundUp(-139.5114223, 5);
roundUp(-0.1998234, 4);
}

private static void roundUp(double value, int decimalPlaces) {
int temp = (int) Math.pow(10, decimalPlaces);
double roundedValue = Math.floor(value * temp) / temp;
System.out.printf("Round floor %12s to %s decimal places = %s%n", value, decimalPlaces, roundedValue);
}
}

Output

Round floor  115.9716123 to 3 decimal places = 115.971
Round floor 91.1236234 to 2 decimal places = 91.12
Round floor -139.5114223 to 5 decimal places = -139.51143
Round floor -0.1998234 to 4 decimal places = -0.1999




See Also