🌐
Scaler
scaler.com › home › topics › math.floor() in java
Math floor() Java | Math.floor() Function in Java - Scaler Topics
May 5, 2024 - The Java Math floor() is a mathematical function available in Java Math Library. This function returns the closest integer value (represented as a double value) which is less than or equal to the given double value.
🌐
Upgrad
upgrad.com › home › tutorials › software & tech › math floor in java
Math floor() Function in Java with Examples | upGrad
1 month ago - Math floor in Java is a mathematical function in the programming language that rounds down a given decimal number to the nearest integer. To be precise, it returns the largest integer less than or equal to the given number while discarding the ...
Discussions

java - Why does Math.floor return a double? - Stack Overflow
Official Javadoc says that Math.floor() returns a double that is "equal to a mathematical integer", but then why shouldn't it return an int? More on stackoverflow.com
🌐 stackoverflow.com
java - calculate Math floor - Stack Overflow
1 My Java math expressions using Math.floor() come out to 0 instead of the intended number, even though my paper calculations say it should be otherwise More on stackoverflow.com
🌐 stackoverflow.com
Why floor, round and ceil return double?
What if the number being passed into the function is higher than the biggest int? More on reddit.com
🌐 r/java
7
6
September 12, 2013
java - (int) Math.sqrt(n) much slower than (int) Math.floor(Math.sqrt(n)) - Stack Overflow
I cannot reproduce the same results. Using this simple Java code below, the function without the call to Math.floor is consistently faster: More on stackoverflow.com
🌐 stackoverflow.com
People also ask

What is the Math floor (-4.7) in Java?
As we know, the Math.floor() function rounds the given number to its nearest integer, which will be less than or equal to the number. Therefore, in this case, the result of the Math.floor(-4.7) is -5 . As we know, the Math.floor() function rounds the given number to its nearest integer, which will be less than or equal to the number. Therefore, in this case, the result of the Math.floor(-4.7) is -5 . How to apply floor in Java?
🌐
upgrad.com
upgrad.com › home › tutorials › software & tech › math floor in java
Math floor() Function in Java with Examples | upGrad
What is Math.floor JavaScript?
Math.floor is a JavaScript function that rounds a number down to the nearest integer, returning the largest integer less than or equal to the given number. Math.floor is a JavaScript function that rounds a number down to the nearest integer, returning the largest integer less than or equal to the given number.
🌐
upgrad.com
upgrad.com › home › tutorials › software & tech › math floor in java
Math floor() Function in Java with Examples | upGrad
How to apply floor in Java?
To apply the floor function in Java, one can simply use the Math.floor() method. One must pass their desired decimal number as an argument to the function to apply it. In return, it will give out the result by rounding it down to the largest integer that is less than or equal to the given number. To apply the floor function in Java, one can simply use the Math.floor() method. One must pass their desired decimal number as an argument to the function to apply it. In return, it will give out the result by rounding it down to the largest integer that is less than or equal to the given number. What
🌐
upgrad.com
upgrad.com › home › tutorials › software & tech › math floor in java
Math floor() Function in Java with Examples | upGrad
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › lang › Math.html
Math (Java Platform SE 8 )
October 20, 2025 - If both arguments are integers, ... to the mathematical result of raising the first argument to the power of the second argument if that result can in fact be represented exactly as a double value. (In the foregoing descriptions, a floating-point value is considered to be an integer if and only if it is finite and a fixed point of the method ceil or, equivalently, a fixed point of the method floor...
🌐
Programiz
programiz.com › java-programming › library › math › floor
Java Math floor()
double a = 3.8; System.out.println(Math.floor(a)); } } // Output: 3.0
🌐
Tutorialspoint
tutorialspoint.com › home › java/lang › java math floor function
Java Math Floor Function
September 1, 2008 - The Java Math 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.
🌐
CodeGym
codegym.cc › java blog › java numbers › java floor() method
Java floor() method
December 5, 2024 - Math.floor() method in Java returns a “double” value equal to the greatest integer less than or equal to the argument. If the given number is already an integer it returns the integer.
Find elsewhere
🌐
W3Schools
w3schools.com › java › ref_math_floor.asp
Java Math floor() Method
Java Examples Java Videos Java Compiler Java Exercises Java Quiz Java Code Challenges Java Server Java Syllabus Java Study Plan Java Interview Q&A Java Certificate · ❮ Math Methods · Round numbers down to the nearest integer: System.out...
🌐
Codecademy
codecademy.com › docs › java › math methods › .floor()
Java | Math Methods | .floor() | Codecademy
November 10, 2022 - The Math.floor() method returns the largest integer value that is less than or equal to the argument. ... Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
🌐
Vultr Docs
docs.vultr.com › java › standard-library › java › lang › Math › floor
Java Math floor() - Round Down Value | Vultr Docs
December 3, 2024 - The Math.floor() method in Java is a crucial tool when it comes to rounding down floating-point numbers to the nearest integer that is less than or equal to the specified number.
🌐
Medium
medium.com › @AlexanderObregon › rounding-numbers-with-math-round-math-floor-and-math-ceil-in-java-d201bbeb85e2
Java’s Math Rounding Methods Explained | Medium
March 7, 2025 - The Math.floor() method rounds a number downward to the next whole number, meaning it moves toward negative infinity. This is a strict truncation method that removes any decimal portion without checking its value.
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-floor-method-examples
Java floor() method with Examples - GeeksforGeeks
January 21, 2026 - The Math.floor() method in Java returns the largest integer value that is less than or equal to a given number. The result is returned as a double and represents the mathematical floor of the argument.
🌐
PREP INSTA
prepinsta.com › home › java tutorial › java math floor() method
Java Math floor() Method | PrepInsta
May 27, 2023 - Java Math floor() Method Returns the largest double value that is less than or equal to the argument and is equal to a mathematical integer.
🌐
Reddit
reddit.com › r/java › why floor, round and ceil return double?
r/java on Reddit: Why floor, round and ceil return double?
September 12, 2013 -

Might be stupid question, but this has always puzzled me about Java.

Why functions Math.floor(), Math.round() and Math.ceil() return double value?

I thought main point of those is to convert floating point number into whole number. So why not returning long or int? That way we don't have to manually cast everytime.

🌐
Codecademy
codecademy.com › forum_questions › 50c386a4a122749bc1006ca6
Math.random and Math.floor explained | Codecademy
December 23, 2012 - Math.random generates a number ... To get it to be a whole number, i.e. an integer, apply Math.floor, which rounds down to the nearest whole number: Math.floor(Math.random() * 10) To get a whole number between 1 and 10, add ...
Top answer
1 of 2
6

The Math.floor method just delegates the call to the StrictMath.floor method (as seen on java.lang.StrictMath's source code). This method is a native method. After this method the cast does not have to do anything because it is already a number that is equal to an integer (so no decimal places).

Maybe the native implementation of floor is faster than the cast of a double value to an int value.

2 of 2
1

I cannot reproduce the same results. Using this simple Java code below, the function without the call to Math.floor is consistently faster:

with floor elapsed milliseconds: 7354
without floor elapsed milliseconds: 4252


public class TestCast {
    private static final int REPS = Integer.MAX_VALUE / 4;

    private static void withFloor() {
        long sum = 0;
        long start = System.currentTimeMillis();
        for (int i = REPS;  i != 0;  --i) {
            sum += (int)Math.floor(Math.sqrt(i));
        }
        long end = System.currentTimeMillis();
        long elapsed = end - start;
        System.out.println("with floor elapsed milliseconds: " + elapsed);
        System.out.println(sum);
    }

    private static void withoutFloor() {
        long sum = 0;
        long start = System.currentTimeMillis();
        for (int i = REPS;  i != 0;  --i) {
            sum += (int)Math.sqrt(i);
        }
        long end = System.currentTimeMillis();
        long elapsed = end - start;
        System.out.println("without floor elapsed milliseconds: " + elapsed);
        System.out.println(sum);
    }

    public static void main(String[] args) {
        withFloor();
        withoutFloor();
    }
}

Also, looking at the disassembled byte code we can clearly see the call to Math.floor in the first function and no call in the second. There must be something else going on in your code. Perhaps you can post your code or a shortened version of it that shows the results that you are seeing.

private static void withFloor();
  Code:
     0: lconst_0      
     1: lstore_0      
     2: invokestatic  #2                  // Method java/lang/System.currentTimeMillis:()J
     5: lstore_2      
     6: ldc           #3                  // int 536870911
     8: istore        4
    10: iload         4
    12: ifeq          35
    15: lload_0       
    16: iload         4
    18: i2d           
    19: invokestatic  #4                  // Method java/lang/Math.sqrt:(D)D
    22: invokestatic  #5                  // Method java/lang/Math.floor:(D)D
    25: d2i           
    26: i2l           
    27: ladd          
    28: lstore_0      
    29: iinc          4, -1
    32: goto          10
    35: invokestatic  #2                  // Method java/lang/System.currentTimeMillis:()J
    38: lstore        4
    40: lload         4
    42: lload_2       
    43: lsub          
    44: lstore        6
    46: getstatic     #6                  // Field java/lang/System.out:Ljava/io/PrintStream;
    49: new           #7                  // class java/lang/StringBuilder
    52: dup           
    53: invokespecial #8                  // Method java/lang/StringBuilder."<init>":()V
    56: ldc           #9                  // String with floor elapsed milliseconds: 
    58: invokevirtual #10                 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
    61: lload         6
    63: invokevirtual #11                 // Method java/lang/StringBuilder.append:(J)Ljava/lang/StringBuilder;
    66: invokevirtual #12                 // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
    69: invokevirtual #13                 // Method java/io/PrintStream.println:(Ljava/lang/String;)V
    72: getstatic     #6                  // Field java/lang/System.out:Ljava/io/PrintStream;
    75: lload_0       
    76: invokevirtual #14                 // Method java/io/PrintStream.println:(J)V
    79: return        

private static void withoutFloor();
  Code:
     0: lconst_0      
     1: lstore_0      
     2: invokestatic  #2                  // Method java/lang/System.currentTimeMillis:()J
     5: lstore_2      
     6: ldc           #3                  // int 536870911
     8: istore        4
    10: iload         4
    12: ifeq          32
    15: lload_0       
    16: iload         4
    18: i2d           
    19: invokestatic  #4                  // Method java/lang/Math.sqrt:(D)D
    22: d2i           
    23: i2l           
    24: ladd          
    25: lstore_0      
    26: iinc          4, -1
    29: goto          10
    32: invokestatic  #2                  // Method java/lang/System.currentTimeMillis:()J
    35: lstore        4
    37: lload         4
    39: lload_2       
    40: lsub          
    41: lstore        6
    43: getstatic     #6                  // Field java/lang/System.out:Ljava/io/PrintStream;
    46: new           #7                  // class java/lang/StringBuilder
    49: dup           
    50: invokespecial #8                  // Method java/lang/StringBuilder."<init>":()V
    53: ldc           #15                 // String without floor elapsed milliseconds: 
    55: invokevirtual #10                 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
    58: lload         6
    60: invokevirtual #11                 // Method java/lang/StringBuilder.append:(J)Ljava/lang/StringBuilder;
    63: invokevirtual #12                 // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
    66: invokevirtual #13                 // Method java/io/PrintStream.println:(Ljava/lang/String;)V
    69: getstatic     #6                  // Field java/lang/System.out:Ljava/io/PrintStream;
    72: lload_0       
    73: invokevirtual #14                 // Method java/io/PrintStream.println:(J)V
    76: return        
🌐
Sarthaks eConnect
sarthaks.com › 3497213 › explain-java-math-floor-method
Explain Java Math floor() Method - Sarthaks eConnect | Largest Online Education Community
LIVE Course for free · The Java Math floor() method is a built-in function in the Java programming language that returns the largest integer value that is less than or equal to the given argument.