In this post we’ll see how to convert float to int in Java.

### Considerations while converting float to int in Java

While converting float to int two things you need to consider are-

- Range of float is more than the int, if the float you are trying to convert to int is out of range for int then how to handle that.
- In float value there are fractional parts where as int doesn’t have fractional part so what to do with fractional part is another thing to consider. Fractional part can be truncated or you need to round it.

### Converting using intValue() method or by type casting

You can convert float to int using `intValue()`

method of the Float class (needs a Float object) or by
explicitly type casting
float value to int. In fact intValue() method implementation also do the type casting and returns the value of this Float
as an int after a *narrowing primitive conversion*. Problem with these two methods is that the *rounding to the
nearest integer doesn’t happen* while converting float to int, fractional part is truncated.

### Converting float to int using intValue() method

public class FloatToInt { public static void main(String[] args) { Float f = 36.89f; int val = f.intValue(); System.out.println("Converted int value- " + val); } }

__Output__Converted int value- 36

As you can see float value is truncated while converting it to int.

### Converting float to int using explicit casting

public class FloatToInt { public static void main(String[] args) { float f = 36.89f; int val = (int)f; System.out.println("Converted int value- " + val); } }

__Output__Converted int value- 36

### Converting float to int with rounding to nearest integer

The above two ways of converting float to int are not rounding the float to the nearest integer which is not what you’d
want in most of the scenarios. In order to do that you should use `Math.round(float a)`

method which returns
the closest int to the passed argument.

Math.round() method also takes care of the special cases-

- If the argument is NaN, the result is 0.
- If the argument is negative infinity or any value less than or equal to the value of Integer.MIN_VALUE, the result is equal to the value of Integer.MIN_VALUE.
- If the argument is positive infinity or any value greater than or equal to the value of Integer.MAX_VALUE, the result is equal to the value of Integer.MAX_VALUE.

### Converting float to int in Java using Math.round()

public class FloatToInt { public static void main(String[] args) { float f = 36.89f; int val = Math.round(f); System.out.println("Converted int value= " + val); } }

__Output__Converted int value= 37

**If passed float value > Integer.MAX_VALUE**

public class FloatToInt { public static void main(String[] args) { float f = 3456644678889f; int val = Math.round(f); System.out.println("Converted int value= " + val); } }

__Output__Converted int value= 2147483647

This is the special case; since the passed float value is greater than or equal to the value of Integer.MAX_VALUE so Integer.MAX_VALUE is returned.

### Example with different float arguments

public class FloatToInt { public static void main(String[] args) { float val1 = 456; float val2 = -123.4545f; float val3 = 1456.8901f; int i1 = Math.round(val1); System.out.println("Converted int value= " + i1); int i2 = Math.round(val2); System.out.println("Converted int value= " + i2); int i3 = Math.round(val3); System.out.println("Converted int value= " + i3); } }

__Output__Converted int value= 456 Converted int value= -123 Converted int value= 1457

That's all for the topic **How to Convert float to int in Java**. If something is missing or you have
something to share about the topic please write a comment.

**You may also like**

- How to Convert double to int in Java
- How to Convert float to String in Java
- How to Convert String to float in Java
- Read PDF in Java Using iText
- Java instanceof Operator With Examples
- DelayQueue in Java With Examples
- Spring Boot Microservices Example
- Python Multi-threading Tutorial
- Spring Bean Scopes
- First React App – Hello World React Example

## No comments:

## Post a Comment