HOWTO · Java

How to Convert Int to Float in Java

This tutorial will demonstrate how to convert integer into float in Java.

On this page

To convert a primitive int to float in Java, assign the integer to a float variable: float converted = value;. Java performs this widening conversion automatically, so an explicit cast is unnecessary. If the value is stored in an Integer wrapper object, you can call its floatValue() instance method instead. Keep in mind that float has limited precision, so some large integer values are rounded during conversion.

Convert an int to float with widening conversion

A primitive int can be assigned directly to a primitive float. Java widens the value as part of the assignment because float can represent a wider range of magnitudes than int. This is a numeric conversion, not a requirement to write a cast. The following complete program shows the usual case with a small value.

Run this self-contained example:

public class Main {
  public static void main(String[] args) {
    int value = 56;
    float converted = value;
    System.out.println(converted);
  }
}

The program prints the converted value using Java’s standard float formatting. The example is a complete Java class with an entry point, so it can be compiled and run as a small standalone program. The conversion itself happens when value is assigned to converted; printing simply displays the resulting value. For a variable that already has type int, the direct assignment is the clearest form.

You may also write float converted = (float) value;, but the cast does not change this conversion. It is redundant for an int-to-float assignment and can make readers think that Java requires explicit casting here. Use the direct assignment unless you have a separate reason to make a conversion explicit for readers of your code.

Use floatValue() with an Integer object

Integer is Java’s wrapper class for the primitive int. When a value is already held by an Integer object, floatValue() returns its value as a primitive float. For example, the expression Integer count = Integer.valueOf(56); float converted = count.floatValue(); converts the wrapped value. Here, floatValue() is an instance method called on count.

This wrapper-object case differs from the direct primitive assignment above. If your variable is already an int, calling a method on an Integer is unnecessary; assign the primitive directly. If a value comes from an API or collection as an Integer, calling floatValue() makes the unboxing and conversion explicit. Java can also unbox an Integer in an assignment to float, but the method can be useful when you want to show the conversion at the point where it occurs. The method does not increase the precision of the destination type: its result is still a float and follows the same precision limits.

Understand precision limits when converting int to float

Although both Java types use 32 bits, they encode numbers differently. An int represents every integer within its range exactly. A float uses a sign, exponent, and significand, leaving 24 bits of significand precision for ordinary finite values. As a result, it can represent every integer exactly only within the contiguous range from −2^24 through 2^24. Beyond that range, representable values become spaced apart, and some integer values round to a nearby float value.

This does not mean every conversion above that threshold changes the value: some larger integers are still representable exactly. The important point is that exact representation is no longer guaranteed for every integer. Widening describes the type conversion in Java; it does not promise that the destination has enough precision to preserve every source value.

If your calculations or identifiers require exact integer values across the full int range, keep them as int or use another exact integer type instead of converting them to float. If you need fractional arithmetic and can tolerate rounding at larger magnitudes, float may be suitable. Choose based on the precision your application needs, and avoid comparing a converted large value as though it must equal its original integer in every case.