Convert int to float in Java
Tags: int float Integer Convert
In this Java core tutorial, we learn how to convert int value into float value in Java via different solutions.
Assign int variable to float variable in Java
In Java to convert a int value to float value we can directly assign int variable to a float variable as Java code below.
ConvertIntToFloatExample1.java
public class ConvertIntToFloatExample1 {
public static void main(String[] args) {
int value1 = 87654;
float value2 = value1;
System.out.println("int value: " + value1);
System.out.println("float value: " + value2);
}
}
int value: 87654
float value: 87654.0
Using Integer.floatValue() method to get float value from an Integer object
With an Integer object in Java we can use the Integer.floatValue() method to return float value from a given Integer object as Java example code below.
ConvertIntToFloatExample2.java
public class ConvertIntToFloatExample2 {
public static void main(String[] args) {
Integer value1 = 123456;
float value2 = value1.floatValue();
System.out.println("int value: " + value1);
System.out.println("float value: " + value2);
}
}
int value: 123456
float value: 123456.0
Happy Coding 😊