Convert long to double in Java

Tags: double long Convert

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

Assign long variable to double variable

ConvertLongToDoubleExample1.java

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

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

Using Double.valueOf() method to create Double from a long value

ConvertLongToDoubleExample2.java

public class ConvertLongToDoubleExample2 {
    public static void main(String[] args) {
        long value1 = 202101;
        double value2 = Double.valueOf(value1);

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

Using Long.doubleValue() method to get double value from a Long object

ConvertLongToDoubleExample3.java

public class ConvertLongToDoubleExample3 {
    public static void main(String[] args) {
        Long value1 = Long.valueOf(30000);
        double value2 = value1.doubleValue();

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

Happy Coding 😊