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

David picture David · Apr 16, 2010 · Viewed 256k times · Source

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

Mihir Mathuria picture Mihir Mathuria · Apr 16, 2010

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