lunes, 29 de agosto de 2011

Java: convert double to int

References:
  • http://forum.java.sun.com/thread.jspa?threadID=609720
Solution:
To convert a double to int, always rounding down, you need to do the following:
int d = (int) x;

or an alternative method

int d = (int) Math.floor(x);

For example, if x was 2.75, with this method d would be 2. We can think about it as if the decimal digits were taken away.

No hay comentarios: