Java Round up to Lower value floor()

Java Round up to Lower value floor()

java.lang.Math class comes with many methods to do simple basic numeric operations. One such one is floor() method which returns always a nearest lower double value of a number.

One advantage of Math class methods is that they are declared as static so that they can be called without the need of object creation.

Following is the Math class signature as defined in java.lang package.

public final class Math extends Object

Following is the method signature as defined in Math class.

  • double floor(double a): Returns the nearest lower double value of the double value passed as parameter.

For example:

1. The values 24.01 and 24.99 lies between 24.0 and 25.0. The floor(24.01) and floor(24.99) returns 24.0

2. The values -24.01 and -24.99 lies between -24.0 and -25.0 where -25.0 is lower. The floor(-24.01) and floor(-24.99) returns -25.0

Observe, the method parameter and return type are the same of double value.

Example on Java Round up to Lower value floor() illustrating the above two methods

public class MathFunctionDemo
{
  public static void main(String args[])
  {
   System.out.println("Math.floor(24.01) is " + Math.floor(24.01));
   System.out.println("Math.floor(24.99) is " + Math.floor(24.99));

   System.out.println("\nMath.floor(-24.01) is " + Math.floor(-24.01));
   System.out.println("Math.floor(-24.99) is " + Math.floor(-24.99));
  }
}


Java Round up to Lower value floor()
Output screen of Java Round up to Lower value floor()

Leave a Comment

Your email address will not be published.