David David - 1 year ago 84
Java Question

Rounding a double to turn it into an int (java)

Right now I'm trying this:

int a = round(n);

is a
but it's not working. What am I doing wrong?

Answer Source

What is the return type of the round() method in the snippet?

If this is the Math.round() method, it returns a Long when the input param is Double.

So, you will have to cast the return value:

int a = (int) Math.round(doubleVar);