Open In App

Java Number.longValue() Method

Last Updated : 15 May, 2025
Summarize
Comments
Improve
Suggest changes
Share
Like Article
Like
Report

The Number.longValue() is an inbuilt method in Java of java.lang package. This method is used to convert any numeric value into a long type. This may involve rounding or truncation.

What is longValue()?

The longValue() method returns the value of a number after converting it to the long data type. Sometimes this involves rounding or truncating decimal points, when the original value is a floating-point number like float or double.

Syntax of Number.longValue() Method

public abstract long longValue()

  • Parameters: This method does not accept any parameters. 
  • Return value: This method returns the numeric value represented by this object after conversion to type long.

Examples of Java Number.longValue() Method

Example 1: In this example, we are going to convert a Float and Double object into a long using the longValue() method.

Java
// Java program to demonstrate
// the use of Number.longValue() method
import java.lang.Number;

public class Geeks {
    
    public static void main(String[] args) {

        // Create a Float object
        Float f = new Float(127483456f);

        // Convert Float to long using longValue()
        long longFromFloat = f.longValue();
        System.out.println("Float to long: " + longFromFloat);

        // Create a Double object
        Double d = new Double(78549876);

        // Convert Double to long using longValue()
        long longFromDouble = d.longValue();
        System.out.println("Double to long: " + longFromDouble);
    }
}

Output
Float to long: 127483456
Double to long: 78549876


Example 2: In this example, we are converting decimal values to long, and we will observe how the decimal portion is truncated in the output.

Java
// Java program to demonstrate
// truncation using longValue() method
import java.lang.Number;

public class Geeks {
    
    public static void main(String[] args) {

        // Create a Float with no decimal part
        Float f = new Float(127f);
        System.out.println("Float to long: " 
        + f.longValue());

        // Create a Double with a decimal part
        Double d = new Double(76.23);
        System.out.println("Double to long: " 
        + d.longValue());
    }
}

Output
Float to long: 127
Double to long: 76


Important Points:

  • This method makes it easy to convert any number like a Float, Double, or Integer into a long. We don’t have to worry about writing extra code to cast or convert manually. Just by calling the method, we get the number in long form.
  • This is very useful when we are working with different number types but want to treat them all the same.
  • When we are working with whole numbers. For example, if you don’t care about the decimal part and just need the whole number like storing prices, counts, or IDs, this method keeps things simple.

Practice Tags :

Similar Reads