java.lang.Math floor(double a)

Description :

This java tutorial shows how to use the floor(double) method of Math class under java.lang package. This method 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.

Method Syntax :

public static double floor(double a)

Parameter Input :

DataType Parameter Description
double a a value

Method Returns :

The floor(double a) method simply returns returns the largest double value that is less than or equal to the argument and is equal to an integer. Special cases:

  • If the argument value is already equal to a mathematical integer, then the result is the same as the argument.
  • If the argument is NaN or an infinity or positive zero or negative zero, then the result is the same as the argument.

Compatibility Version :

Requires Java 1.0 and up

Exception :

N/A

Java Code Example :

This java example source code demonstrates the use of floor(double a) method of Math class. Basically we just get the floor value of a double variable coming from the user input.

package com.javatutorialhq.java.examples;

import static java.lang.System.*;

import java.util.Scanner;

/*
 * This example source code demonstrates the use of 
 * floor(double a) method of Math class
 * Get the floor value of the user input
 */

public class MathFloor {

	public static void main(String[] args) {
		// ask for user input
		out.print("Enter a value:");
		Scanner scan = new Scanner(System.in);
		// use scanner to get user console input
		double value = scan.nextDouble();		
		// get the ceiling of a value
		double floorValue = Math.floor(value);
		out.println("floor of "+value+" = "+floorValue);		
		// close the scanner object to avoid memory leak
		scan.close();

	}

}

Sample Output :

Running the abs(double a) method example source code of Math class will give you the following output

Enter a value:132.32
floor of 132.32 = 132.0

Exception Scenario :

N/A

Similar Method :

  • N/A

Suggested Reading List :

References :