# converting a decimal to an int

• 10-12-2009, 08:15 AM
shuks
converting a decimal to an int
Hi, good to be here.

I am writing a program that simulates a projectile's trajectory. I have the mathematical equation to do so but I have a problem when I try to implement graphics.

The problem is that when I compute the math the resulting X and Y co-ordanints of the projectile ends up in a long decimal..

for example the X co-ordanint might be 9.2938 and the Y co-ordanint might be 17.3726.

The graphical framework I'm using only excepts integers when drawing objects to the screen so I can't use the exact position of the X and Y co-ordanints. This model does not have to be extremely accurate so I figure the easy way to solve this problem is to round the number with "Math.round()" function.

This is math equation I'm trying to round...

yPos = Math.round((startHeight + velocity * Math.sin(angle) * time - gravity * time * time/2));

xPos = Math.round((startLength + velocity * Math.cos(angle) * time));

when I try to do this I get an error saying, "Possible loss of precision. found: long, required: int"

All the variables in this equation are type "double", and the xPos and Ypos variables are type int...

Can someon please enlighten me on the situation?
• 10-12-2009, 08:25 AM
r035198x
Quote:

Originally Posted by shuks
....

All the variables in this equation are type "double", and the xPos and Ypos variables are type int...

Can someon please enlighten me on the situation?

Sure. The Math.round method that takes a double returns a long.
• 10-12-2009, 08:26 AM
If accuracy is not a problem, why not just

Code:

```double d = 5.43764826; int i = (int) d;```
Edit: And, if you want it to round up at .5 or higher (as is common in standard mathematics) just do
Code:

```double d = 5.43764826; int i = (int) (d + 0.5);```
Assuming, of course, that the values are all between Integer.MIN_VALUE and Integer.MAX_VALUE.
• 10-12-2009, 08:31 AM
Quote:

Originally Posted by shuks
Ok, I changed all the variables to type "float". The Math.round() function is supposed to take a float and return an int, but this is not working??

Don't do that. Why would you want to? Simply cast the things.
• 10-12-2009, 08:34 AM
Using "your" code

Simply rounding everything down
Code:

```yPos = (int) (startHeight + velocity * Math.sin(angle) * time - gravity * time * time/2); xPos = (int) (startLength + velocity * Math.cos(angle) * time);```
rounding up at .5 or higher
Code:

```yPos = (int) ((startHeight + velocity * Math.sin(angle) * time - gravity * time * time/2) + 0.5); xPos = (int) ((startLength + velocity * Math.cos(angle) * time) + 0.5);```
• 10-12-2009, 08:46 AM
shuks
Thanks both of you for the advice. I will try casting it as an int. I will post the results when I get it done. Hopefully it works
• 10-12-2009, 09:05 AM
shuks
Worked great!! thanks. Such a simple solution, I'm embarrassed I didnt try that before :p
• 10-12-2009, 09:26 AM
r035198x
Quote:

Originally Posted by shuks
... Such a simple solution, I'm embarrassed I didnt try that before :p