# Thread: converting a decimal to an int

1. Member
Join Date
Oct 2009
Posts
18
Rep Power
0

## 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?

2. Senior Member
Join Date
Aug 2009
Posts
2,388
Rep Power
7
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.

3. Senior Member
Join Date
Jun 2008
Posts
2,565
Rep Power
8
If accuracy is not a problem, why not just

Java 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
Java 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.
Last edited by masijade; 10-12-2009 at 09:30 AM.

4. Senior Member
Join Date
Jun 2008
Posts
2,565
Rep Power
8
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.

5. Senior Member
Join Date
Jun 2008
Posts
2,565
Rep Power
8
Using "your" code

Simply rounding everything down
Java 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
Java 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);```

6. Member
Join Date
Oct 2009
Posts
18
Rep Power
0
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

7. Member
Join Date
Oct 2009
Posts
18
Rep Power
0
Worked great!! thanks. Such a simple solution, I'm embarrassed I didnt try that before :p

8. Senior Member
Join Date
Aug 2009
Posts
2,388
Rep Power
7
Originally Posted by shuks
... Such a simple solution, I'm embarrassed I didnt try that before :p
It's one at a time, no jumping the queue.
Last edited by r035198x; 10-12-2009 at 10:42 AM.

9. Member
Join Date
Oct 2009
Posts
18
Rep Power
0
Huh?? what is a defenestration time?

10. Senior Member
Join Date
Aug 2009
Posts
2,388
Rep Power
7
Defenestration - Wikipedia, the free encyclopedia

Watch out for the lacerations when your turn comes.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•