According to the same Javadoc:

If the argument is NaN or an infinity or positive zero or negative zero, then the result is the same as the argument. Can't do that with an int.

The largest double value is also larger than the largest int, so it would have to be a long.

Answer from krosenvold on Stack Overflow
🌐
W3Schools
w3schools.com › java › ref_math_floor.asp
Java Math floor() Method
Note: For positive numbers the floor() method just removes the decimal part, but for negative numbers the integer part of the number will be changed if the number has a decimal part.
Discussions

java - (int) Math.floor(x / TILESIZE) or just (int) (x / TILESIZE) - Game Development Stack Exchange
This means that I can replace the (int) Math.floor with just x / 64. But if I run this on a different OS do you think it might give a different result? I'm just afraid there might be some case where this would round up and not down. Should I keep doing it the way I was and maybe make a function like convert(int i) to make it easier? Or is it OK to just do x / 64? ... \$\begingroup\$ Casting a float with (int) in java ... More on gamedev.stackexchange.com
🌐 gamedev.stackexchange.com
October 6, 2012
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 - 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
java - (int) Math.sqrt(n) much slower than (int) Math.floor(Math.sqrt(n)) - Stack Overflow
The JVM might replace all of them by an intrinsic function (i.e. CPU instruction) at runtime. 2013-11-28T16:00:41.003Z+00:00 ... 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
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-floor-method-examples
Java floor() method with Examples - GeeksforGeeks
January 21, 2026 - Example 1: This program demonstrates how the Math.floor() method rounds different types of double values down to the nearest integer in Java.
🌐
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.

🌐
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.
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › lang › Math.html
Math (Java Platform SE 8 )
October 20, 2025 - 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. If the argument value is less than zero but greater than -1.0, then the result is negative zero. Note that the value of Math.ceil(x) is exactly the value of -Math.floor...
Find elsewhere
🌐
Sololearn
sololearn.com › en › Discuss › 2886933 › in-java-how-do-we-make-a-type-double-round-up-to-the-nearest-integer
In Java, how do we make a type double round UP to the nearest integer? | Sololearn: Learn to code for FREE!
javarounding · 21st Sep 2021, 12:38 AM · Solus · 9 Answers · Answer · + 3 · Math.round(); function rounds to the nearest integer. Math.ceil(); function rounds to nearest higher integer Math.floor(); rounds to nearest lower integer I hope ...
🌐
Narkive
comp.lang.java.help.narkive.com › gcS5laFk › math-floor
Math.Floor
Post by Hal Rosser Math.floor(...) ... for Math.round() says the method is equal to (long)Math.floor(a + 0.5d) Concept - the floor function will take the integer part of the number....
🌐
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() function in Java is a powerful tool when accuracy and precision are needed in processing numerical data, ensuring results are consistently rounded down to the nearest integer.
🌐
Upgrad
upgrad.com › home › tutorials › software & tech › math floor in java
Math floor() Function in Java with Examples | upGrad
1 month ago - In Java, there are several functions to work on mathematical calculations. These are predefined functions that accept value and return results. One such handy mathematical function is the Math Floor Java which allows you to round a given decimal number to its nearest whole number or integer, similar to the Math.ceil Java method.
🌐
Programiz
programiz.com › java-programming › library › math › floor
Java Math floor()
That is, the value 3.8 will be rounded to 3.0 which is equal to integer 3. class Main { public static void main(String[] args) { double a = 3.8; System.out.println(Math.floor(a)); } } // Output: 3.0
🌐
Scaler
scaler.com › home › topics › math.floor() in java
Math floor() Java | Math.floor() Function in Java - Scaler Topics
May 5, 2024 - For example, if we pass 5.67 to the Math floor() function, we will get 5.0 as the output. ... In the above example, we are rounding off the double value a=5.987654 to its immediate smaller integer (as a double value) 5.0 using the Java Math ...
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        
🌐
Codecademy
codecademy.com › docs › java › math methods › .floor()
Java | Math Methods | .floor() | Codecademy
November 10, 2022 - In the case of double values, Math.floor() returns the next integer value below the provided double value. In the case of integers provided, these will return the same value as provided: ... Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more! ... Learn to code in Java — a robust programming language used to create software, web and mobile apps, and more.
🌐
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.
Top answer
1 of 2
3

Just do cast to int

return (int) n;

To understand the problem with your code is that n is casted to int before the division n / 1000 so that makes an int division, you need more parenthesis, also multiply by 1000 and not 10000

return (int) (n / 1000 * 1000);
       
2 of 2
0

Simple divide and cast to an integer value. Use long to achieve greater range for your numbers.

int res = (int) floatValue;
long res = (long) floatValue;

Java will always round towards zero.

  • If the number's greater than zero, it'll round down.
  • If the number's less than zero, it'll round up.

The trick you're 'using' helps with achieving rounding to certain digits. Again, use double for improved accurarcy and range.

public class RoundWithDigits {

    public static void main(final String[] args) {
        final double[] testNumbers = new double[] { -3, -2, -1, 0, 1, 2, 3, 5343554, 15.7 };

        for (int preserveDigits = 0; preserveDigits < 5; preserveDigits++) {
            System.out.println("Preserving digits: " + preserveDigits);
            for (int i = 0; i < testNumbers.length + 5; i++) {
                final double randomNumber = (i < testNumbers.length) ? testNumbers[i] : Math.random() * 100 - 50;
                System.out.println("\tRandom:\t" + randomNumber);
                System.out.println("\tRound:\t" + round(randomNumber, preserveDigits));
                System.out.println("\tRTZero:\t" + roundTowardsZero(randomNumber, preserveDigits));
                System.out.println("\tRUp:\t" + roundCeiling(randomNumber, preserveDigits));
                System.out.println("\tRDown:\t" + roundFloor(randomNumber, preserveDigits));
                System.out.println();
            }
            System.out.println();
        }
        System.out.println("All done.");
    }



    static public double round(final double pValue, final int pPreserveDigits) {
        final long factor = (long) Math.pow(10, pPreserveDigits);
        final long roundedMult = Math.round(pValue * factor);
        return (double) roundedMult / factor;
    }
    static public double roundTowardsZero(final double pValue, final int pPreserveDigits) {
        final long factor = (long) Math.pow(10, pPreserveDigits);
        final long roundedMult = (long) (pValue * factor);
        return (double) roundedMult / factor;
    }
    static public double roundCeiling(final double pValue, final int pPreserveDigits) {
        final long factor = (long) Math.pow(10, pPreserveDigits);
        long roundedMult = (long) (pValue * factor);
        if (roundedMult != pValue * factor && pValue > 0) ++roundedMult;
        return (double) roundedMult / factor;
    }
    static public double roundFloor(final double pValue, final int pPreserveDigits) {
        final long factor = (long) Math.pow(10, pPreserveDigits);
        long roundedMult = (long) (pValue * factor);
        if (roundedMult != pValue * factor && pValue < 0) --roundedMult;
        return (double) roundedMult / factor;
    }

}
🌐
CodeAhoy
codeahoy.com › java › Math-Floor-method-JI_14
Java Math.floor() Method with Examples | CodeAhoy
October 12, 2019 - public class MathEx { public static void main(String[] args) { floor(); } private static void floor() { System.out.println(Math.floor(3.6)); // 3.0 System.out.println(Math.floor(3.9)); // 3.0 System.out.println(Math.floor(7.0)); // 7.0 System.out.println(Math.floor(-7.0)); // -7.0 // cast to int System.out.println((int) Math.floor(192.456)); // 192 System.out.println(Math.floor(Double.NaN)); // NaN } } ... Here’s a screenshoot of the code above if you’re on mobile and having trouble reading the code above. 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. If you need more detailed information, please see Javadocs.
🌐
W3Schools
w3schools.com › jsref › jsref_floor.asp
W3Schools.com
❮ Previous JavaScript Math Object ... Math.floor(-5.1); let f = Math.floor(-5.9); Try it Yourself » · The Math.floor() method rounds a number DOWN to the nearest integer....