How can I convert between ints and doubles?
If exp is an expression denoting an int, it can be converted to a double by means of a so-called “cast” expression: (double) exp If exp is an expression denoting a double, it can be converted to an int by means of a “cast” expression that truncates the information after the decimal point: (int) exp Here are some examples: Java Expression Value (double) 17 17.0 (int) 3.141 3 (int) 5.99 5 (int) -3.141 -3 (int) -5.99 -5 In practice, converting an int to a double is rarely needed, since most binary operators (e.g. +, -, * , /, <, >, ==, !=) automatically convert an int argument to a double when the other argument is a double. (But see the question about dividing two ints, below.) Here are some examples involving addition: Java Expression Value 10 + 3 13 10.9 + 3.141 14.041 10.9 + 3 13.9 10 + 3.141 14.141 ((int) 10.9) + 3 13 (int) (10.9 + 3.141) 14.141 (int) 10.9 + 3.141 13.141 2.5 + 3.5 6.0 Note in the example (int) 10.9 + 3.141 that (int) binds more strongly than +; that is, the above exa