Convert double to long in Java

Tags: double long Convert

In this Java core tutorial, we learn how to convert double value into long value in Java via different solutions.

How to cast a double to long value

ConvertDoubleToLongExample1.java

public class ConvertDoubleToLongExample1 {
    public static void main(String[] args) {
        double value1 = 12.34;
        long value2 = (long)value1;

        System.out.println("double value: " + value1);
        System.out.println("long value: " + value2);
    }
}
The output is:
double value: 12.34
long value: 12

Using Double.longValue() method to get long value from a Double object

ConvertDoubleToLongExample2.java

public class ConvertDoubleToLongExample2 {
    public static void main(String[] args) {
        Double value1 = Double.valueOf(76.55);
        long value2 = value1.longValue();

        System.out.println("double value: " + value1);
        System.out.println("long value: " + value2);
    }
}
The output is:
double value: 76.55
long value: 76

Happy Coding 😊