java tutorial - Java Math.floor() Method - java programming - learn java - java basics - java for beginners



Java numbers floor

Learn Java - Java tutorial - Java numbers floor - Java examples - Java programs

Description

Method Math.floor () method gives an integer with a zero fractional part closest to the number of the argument to the left, in other words rounding the fraction to the smaller side.

Syntax

The method has the following options

double floor(double d)
double floor(float f)
click below button to copy the code. By - java tutorial - team

Options

Detailed information about the parameters:

  • Primitive data type is double or float.

Return value

  • In Java, Math.floor () returns an integer that is closest to the number of arguments on the left. Returns as a double.

Sample Code

public class Test { 

   public static void main(String args[]) {
      double d1 = 0.4;
      double d2 = -7.75;
      double d3 = 8.6;
      double d4 = 1;
      double d5 = -1;
      
      float f1 = -0.4f;
      float f2 = -5.75f;
      float f3 = 3.4f;
      float f4 = 7;
      float f5 = -7;

      System.out.println("d1 = " + Math.floor(d1));
      System.out.println("d2 = " + Math.floor(d2));
      System.out.println("d3 = " + Math.floor(d3)); 
      System.out.println("d4 = " + Math.floor(d4)); 
      System.out.println("d5 = " + Math.floor(d5)); 
      System.out.println("--------------");  
      System.out.println("f1 = " + Math.floor(f1)); 
      System.out.println("f2 = " + Math.floor(f2));
      System.out.println("f3 = " + Math.floor(f3));
      System.out.println("f4 = " + Math.floor(f4)); 
      System.out.println("f5 = " + Math.floor(f5));
   }
}
click below button to copy the code. By - java tutorial - team

Output

d1 = 0.0
d2 = -8.0
d3 = 8.0
d4 = 1.0
d5 = -1.0
--------------
f1 = -1.0
f2 = -6.0
f3 = 3.0
f4 = 7.0
f5 = -7.0

Related Searches to Java Math.floor() Method