Java
Programming
Data Types
Float to Int
Coding Tutorial

How to convert float to int with Java

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

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:

java
float myFloat = 9.7f;
int myInt = (int) myFloat;  // myInt will be 9

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:

java
float myFloat = -9.7f;
int myInt = (int) myFloat;  // myInt will be -9

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:

java
1float myFloat = 9.7f;
2
3int rounded = Math.round(myFloat);  // rounded will be 10
4int ceiled = (int) Math.ceil(myFloat);  // ceiled will be 10
5int floored = (int) Math.floor(myFloat);  // floored will be 9

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:

MethodInputOutput
Simple Casting9.7f9
Simple Casting-9.7f-9
Math.round()9.7f10
Math.ceil()9.7f10
Math.floor()9.7f9
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
Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.