views:

1629

answers:

1

I used the following line to convert float to int, but not as accurate as I'd like :

 float a=8.61f;
 int b;

  b=(int)a;

The result is : 8 [ It should be 9 ]

When a=-7.65f, the result is :  -7 [ It should be -8 ]

What's the best way to do it ?

+11  A: 

Use Math.round() before typecasting using (int) should round the float to the nearest whole number.

Tom Woolfrey
Yeah. I redid my work in my head. You are correct.
Thomas Owens