Important Notice: Our web hosting provider recently started charging us for additional visits, which was unexpected. In response, we're seeking donations. Depending on the situation, we may explore different monetization options for our Community and Expert Contributors. It's crucial to provide more returns for their expertise and offer more Expert Validated Answers or AI Validated Answers. Learn more about our hosting issue here.

How can I convert between ints and doubles?

convert doubles ints
0
Posted

How can I convert between ints and doubles?

0

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

Related Questions

What is your question?

*Sadly, we had to bring back ads too. Hopefully more targeted.

Experts123