David David - 1 month ago 5
Java Question

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

Right now I'm trying this:

int a = round(n);


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

Answer

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);
Comments