Convert Int to Float in Java

Lovey Arora Jul 27, 2021
  1. Use the floatValue() Function to Convert an Integer Into a Float in Java
  2. Use the Typecasting Method to Convert an Integer Into a Float in Java
Convert Int to Float in Java

Float values represent decimal point numbers in Java. It is one of the fundamental data types used in almost all programming languages.

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

Use the floatValue() Function to Convert an Integer Into a Float in Java

The floatValue() function can convert a given integer value into float. It belongs to the java.lang.Integer class.

For example,

import java.lang.Integer;

public class Main {
    public static void main(String args[])
    {
        Integer a = new Integer(56);

        float b = a.floatValue();

        System.out.println(b);
    }
}

Output:

56.0

Use the Typecasting Method to Convert an Integer Into a Float in Java

Typecasting is a method in which you convert one type of data type into some other type explicitly. By using this technique, we can convert an integer value into a float value.

To typecast an integer value to float, we will use the float keyword.

See the code given below.

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

Output:

56.0

Related Article - Java Float

Related Article - Java Integer