How to change float to int in Java

Asked 2 years ago, Updated 2 years ago, 130 views

 float a=8.61f;
 int b;

 b=(int)a;

This is how you change float to int. It's not accurate.

The value of b is 8, but it's supposed to be 9. And when the value of a is -7.65f, the result is -7, not -8

What should I do to properly change my brother?

floating-point java int

2022-09-22 22:19

1 Answers

Use Math.round(). Round the float to the nearest whole number.


2022-09-22 22:19

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.