LAST UPDATED: SEPTEMBER 1, 2020
Java Float longValue() Method
Java longValue()
method belongs to the Float
class of the java.lang
package and is inherited from the Number
class. This method returns the long equivalent of the Float object after a primitive narrowing conversion(Conversion of a higher type to lower type).
In short, this method is used to convert a Float object into a long value.
Syntax:
public long longValue()
Parameter:
No parameter is passed in this method.
Returns:
The long equivalent of the Float object that is created after conversion.
Example 1:
Here, using the longValue()
method, the Float object is converted into its long equivalent.
import java.lang.Float;
public class StudyTonight
{
public static void main(String[] args)
{
//converting Float object into long
Float x = 15.90f;
long i=x.longValue();
System.out.println("Long value is " +i);
Float y = 67.41f;
long d = y.longValue();
System.out.println("Long value is " +d);
}
}
Long value is 15
Long value is 67
Example 2:
Here is a user-defined example where anyone using this code can put a value of his choice and get the equivalent long 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);
float i = sc.nextFloat();
Float n = i ;
long val = n.longValue(); //converting Float object into long
System.out.println("Long Value is: " + val);
}
catch(Exception e)
{
System.out.println("not a valid float");
}
}
}
Enter the value to be converted : 94.90
Long Value is: 94
*******************************************
Enter the value to be converted : 0X900.9
not a valid float
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.