LAST UPDATED: SEPTEMBER 3, 2020
Java Long floatValue() Method
Java floatValue()
method belongs to the Long
class of the java.lang
package. This method returns the floating equivalent of the Long
after a widening primitive conversion(Conversion of a lower data type into a higher data type).
In short, this method is used to convert a Long object into a primitive float value.
Syntax:
public float floatValue()
Parameter:
No parameter is passed in this method.
Returns:
The float equivalent of the Long object that is created after conversion.
Example 1:
Here, using the floatValue()
method, the Long object is converted into its float equivalent.
import java.lang.Long;
public class StudyTonight
{
public static void main(String[] args)
{
//converting Long object into float
Long x = 65L;
float i = x.floatValue();
System.out.println(i);
Long y = 90L;
float d = y.floatValue();
System.out.println(d);
}
}
65.0
90.0
Example 2:
Here is a user-defined example where anyone using this code can put a value of his choice and get the equivalent float value.
import java.util.Scanner;
public class StudyTonight
{
public static void main(String[] args)
{
try
{
System.out.print("Enter the value to be converted : ");
Scanner sc = new Scanner(System.in);
long i = sc.nextLong();
Long n = i ;
float val = n.floatValue(); //converting Long object into float
System.out.println("Float Value is: " + val);
}
catch(Exception e)
{
System.out.println("not a valid long");
}
}
}
Enter the value to be converted : 75
Float Value is: 75.0
********************************************
Enter the value to be converted : 0x665
not a valid long
Live Example:
Here, you can test the live code example. You can execute the example for different values, even can edit and write your examples to test the Java code.