How to convert float to int in Java?

May 17, 2018

You can convert a float to an int in Java by using the round method Math.round(float):

The method rounds the float to the closest int value.

Other methods that you can use to convert a float to int are:

  • (int) Math.ceil(float) – returns the smallest int which is greater than or equal to the method parameter. As the Math.ceil() method returns the double value by default, it needs to be cast to an int
  • (int) Math.floor(float) – returns the largest int which is less than or equal to the method parameter. As the Math.floor() method returns the double value by default, it needs to be cast to an int
  • (int) float – casting a float to an int removes the decimal fraction of the float

Let me explain you these methods in this blog post.

Math.round(float)

The method to convert a float to an int which is the best in most of the cases is the Math.round(float) method. The method rounds the float to the closest integer value. You can find the description of the method in the table below.

Method header public static int round(float f)
Parameters float f – parameter to be rounded to the closest integer
Returns int – parameter of the method rounded to the closest int
Math.round(float) example

Let me show you an example how you can use the Math.round(float) method.

The output of the code is:

Let me demonstrate what kind of results the method Math.round(float) produces for different method parameters.

f Math.round(f)
-10.93f -11
-10.5f -10
-10.13f -10
10.13f 10
10.5f 11
10.93f 11
Float.NaN 0
<= Integer.MIN_VALUE Integer.MIN_VALUE
>= Integer.MAX_VALUE Integer.MAX_VALUE

 

(int) Math.ceil(float)

To convert a float to an int you can use as well the (int) Math.ceil(float) method. The method will return the smallest int which is greater than or equal to the float parameter. We need to cast the value returned by the method to an int as the method Math.ceil() returns a double value.

(int) Math.ceil(float) example

Let me show you the example, how we can use this method.

The output of the code is:

Below you find the results that the method will produce for different parameters.

f (int) Math.ceil(f)
-10.93f -10
-10.5f -10
-10.13f -10
10.13f 11
10.5f 11
10.93f 11
Float.NaN 0

Note: In the Java documentation you can find that by default the Math.ceil(double) method takes a double as a parameter. Designers of the Math class didn’t decide to create another method to handle float parameters. The reason for that is that all the float parameters will produce the correct results when passed to the Math.ceil(double) method.

 

(int) Math.floor(float)

Another technique you can use to convert a float to an int is to use (int) Math.floor(float). The method returns the largest int which is smaller than or equal to the method argument. Note that we need to use casting to an int as the method Math.floor() returns the double value.

(int) Math.floor(float) example
The output is:
To find out what is the result of the result of using the method for different parameters, check the table below.

f (int) Math.floor(f)
-10.93f -11
-10.5f -11
-10.13f -11
10.13f 10
10.5f 10
10.93f 10
Float.NaN 0

 

Cast float to int

Another method to convert a float to an int is to cast it. The method will just remove the decimal fraction of the float.

Cast float to int example

Let me show you the code example how you can cast a float to an int.

The output of executing the method is:

Below you can find a detailed results, what kind of results cast a float to an int gives.

f (int) f
-10.93f -10
-10.5f -10
-10.13f -10
10.13f 10
10.5f 10
10.93f 10
Float.NaN 0

 

Summary

As you can see there are many methods to convert a float to an int. There is no universal method which will be the best in all the cases. You need to decide what kind of conversion you want to have and pick the method which suits your needs the best.

Click here to add a comment

Leave a comment:


Privacy Preference Center

Necessary

Advertising

Analytics

Other