Given a Double real number. Write a Java program to convert the given double number into a Long (long) in Java.
Examples:
Input: double = 2545.241 Output: 2545 Input: double = 21.54 Output: 21
Double data type: The double data type is a double-precision 64-bit IEEE 754 floating-point. Its value range is endless. It is commonly used for decimal values. You never used double data type for storing precise values, like currency. Its default value is 0.0d.
Example: double double1 = 111.5d
Long data type: The long data type is a 64-bit two’s complement integer. It represents an unsigned64-bit long with a minimum value of 0 and a maximum value of 264-1. The long class also contains methods for doing arithmetic operations on unsigned long. The default value is 0.
Example: long long1 = 111
Approaches:
There can be many approaches to converting double data to long data types. Some of them are listed below:
- Using Typecasting.
- Using Double.longValue() method.
- Using Math.round() method.
- Using Long.parseLong() along with String methods.
Approach 1 – Using Typecasting
This is the simplest method to convert double to long data type in Java.
Syntax:
double doubleVar = 3452.345 long longVar = (long)doubleVar;
Example:
Java
// Java program to convert double value to // long using TypeCasting method class GFG { public static void main(String[] args) { double doubleValue = 4512.412 ; System.out.println( "Double Value: " + doubleValue); // TypeCasting to convert double to long long longValue = ( long )doubleValue; System.out.println( "Long Value: " + longValue); } } |
Double Value: 4512.412 Long Value: 4512
Approach 2 – Using Double.longValue() method
This approach is similar to the above approach. Here the Double class contains a longValue() method, which removes all the digits after decimal from a double value.
Syntax:
double doubleValue = 4512.412; Double newDouble = new Double(doubleValue); long longValue = newDouble.longValue();
Example:
Java
// Java program to convert double value to // long using Double.longValue() method class GFG { public static void main(String[] args) { double doubleValue = 4512.412 ; System.out.println( "Double Value: " + doubleValue); // Create wrapper around the double value Double newDouble = new Double(doubleValue); // Convert double to long long longValue = newDouble.longValue(); System.out.println( "Long Value: " + longValue); } } |
Output:
Double Value: 4512.412 Long Value: 4512
Approach 3 – Using Math.round() method
Math.round() accepts a double value and converts it into the nearest long value by adding 0.5 and removing its decimal points.
Syntax:
long Math.round(Double double1);
Example:
Java
// Java program to convert Double to long // using Math.round() public class GFG { public static void main(String args[]) { // Get the double value double double1 = 99.4 ; System.out.println( "Double Value 1: " + double1); // convert double into long long long1 = ( long )Math.round(double1); // print the long value System.out.println( "Long Value 1: " + long1); double double2 = 99.6 ; System.out.println( "Double Value 2: " + double2); // convert double into long long long2 = ( long )Math.round(double2); // print the long value System.out.println( "Long Value 2: " + long2); } } |
Double Value 1: 99.4 Long Value 1: 99 Double Value 2: 99.6 Long Value 2: 100
Note: In the above example Math.round() function rounds the Double value to the nearest Long Value. For example 99.4 rounds to 99 while 99.6 rounds to 100.
Approach 4 – Using Long.parseLong() along with String Methods
We can parse a string to a long using Long.parseLong() method. So we can convert the given double value to string first, then check for the decimal in the string. If decimal is present, trim the string else, leave the string as it is, then convert the string to a Long value.
Syntax:
long longValue = Long.parseLong(string);
Example:
Java
// Java program to convert double value to // long using Long.parseLong() method class GFG { public static void main(String[] args) { double doubleValue = 4512.412 ; System.out.println( "Double Value: " + doubleValue); // Convert double to string String str = String.valueOf(doubleValue); // Get the index of decimal (.) in the string int index = str.indexOf( '.' ); // Trim the string if needed str = str.substring( 0 , index == - 1 ? str.length() : index); // Convert the string to long long longValue = Long.parseLong(str); // print long Value System.out.println( "Long Value: " + longValue); } } |
Double Value: 4512.412 Long Value: 4512