0 votes
1 view
in Java by (2.6k points)

I used the following line to convert float to int, but it's 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 ?

1 Answer

0 votes
by (46.1k points)
Applying Math.round() will loop the float to the most proximal integer.

Related questions

0 votes
1 answer
asked Sep 25, 2019 in Python by Sammy (47.8k points)
0 votes
1 answer
asked Aug 6, 2019 in Java by Anvi (10.2k points)
0 votes
1 answer
0 votes
1 answer
asked Jul 26, 2019 in Java by Shubham (3.9k points)
Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...