Search code examples
javafloor-division

Java - How to do floor division?


I know that in Python you can do floor division like this:

5 // 2 #2

The // is used for something totally different in Java. Is there any way to do floor division in Java?


Solution

  • You can do

    double val = 5 / 2;
    int answer = Math.floor(val);
    

    OR

    int answer = Math.floorDiv(5, 2);
    

    If you were to call System.out.println(answer); the output would be

    2