Java - How to do floor division?

user10654536 picture user10654536 · Dec 19, 2018 · Viewed 35.1k times · Source

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?

Answer

A J picture A J · Dec 20, 2018

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