How to convert float to int with Java
Data Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.
In Java, converting a float to an int is a common operation, which might seem straightforward but involves some considerations given the differences between these two data types. A float in Java is a single-precision 32-bit IEEE 754 floating point, which means it is capable of holding a wide range of values, including those with fractional parts. Conversely, an int is a 32-bit signed integer, which does not accommodate any fractional part.
Understanding Type Casting
The primary method for converting a float to an int in Java is through type casting. Type casting is the method by which a variable of one type is converted into another type. In Java, there are two types of casting:
- Widening Casting (Implicit): converting a smaller type to a larger size type
- Narrowing Casting (Explicit): converting a larger size type to a smaller size
Converting a float to an int is an example of narrowing casting because you go from a larger precision type to a smaller precision type. This requires explicit casting because it may lead to loss of information, and Java needs the programmer to acknowledge this conversion explicitly.
Performing the Conversion
You can convert a float to an int explicitly in Java by prefixing the float variable with (int). This method simply truncates the decimal part, effectively rounding towards zero. Here's an example:
In this example, myFloat is 9.7. When it's cast to an int, the decimal .7 is truncated, leaving 9.
Issues with Simple Casting
While simple type casting works, it can be problematic for handling floats that are negative or have a significant decimal component. Since simple casting just removes the decimal part, it can lead to unexpected results if not carefully handled:
In other scenarios, you might need more control over how the float is rounded. For precise control over rounding, you can use:
Math.round(float): Rounds the float to the nearest int.Math.ceil(float): Rounds the float up to the nearest int.Math.floor(float): Rounds the float down to the nearest int.
Examples of Controlled Rounding
Here's how you can use these methods:
Notice that Math.round() returns an int directly, while Math.ceil() and Math.floor() return a float, which still needs to be cast to an int explicitly.
Summary Table
The following table summarizes the methods and outcomes based on different float inputs:
| Method | Input | Output |
| Simple Casting | 9.7f | 9 |
| Simple Casting | -9.7f | -9 |
| Math.round() | 9.7f | 10 |
| Math.ceil() | 9.7f | 10 |
| Math.floor() | 9.7f | 9 |
| Math.round() | -9.7f | -10 |
| Math.ceil() | -9.7f | -9 |
| Math.floor() | -9.7f | -10 |
Conclusion
While converting a float to an int in Java is technically simple, understanding the implications of different rounding mechanisms and choosing the right one based on the context is crucial. The methods present in Java—simple type casting, Math.round(), Math.ceil(), and Math.floor()—provide flexibility for different use cases. When in doubt, consider how each of these methods handles both positive and negative values to prevent bugs and unexpected behavior in your applications.
Related reading
- How to convert hashmap to JSON object in Java
- How to convert int[] into List<Integer> in Java?
- How to convert Java String into byte[]?
- How to convert java.sql.timestamp to LocalDate java8 java.time?
- How to convert java.util.Date to java.sql.Date?
- How to convert jsonString to JSONObject in Java
- How to convert List to Map?
- How to convert Milliseconds to X mins, x seconds in Java?
System design interview problems on this topic
- Design an API Rate Limiter: solution and walkthrough
- Design a Distributed Messaging System: solution and walkthrough
- Design an Online Payment Service: solution and walkthrough
- Design a Key Value Store: solution and walkthrough

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseData Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.