Java Program to Convert Double to Long
Last Updated :
30 Jan, 2022
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
class GFG {
public static void main(String[] args)
{
double doubleValue = 4512.412 ;
System.out.println( "Double Value: " + doubleValue);
long longValue = ( long )doubleValue;
System.out.println( "Long Value: " + longValue);
}
}
|
Output
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
class GFG {
public static void main(String[] args)
{
double doubleValue = 4512.412 ;
System.out.println( "Double Value: " + doubleValue);
Double newDouble = new Double(doubleValue);
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
public class GFG {
public static void main(String args[])
{
double double1 = 99.4 ;
System.out.println( "Double Value 1: " + double1);
long long1 = ( long )Math.round(double1);
System.out.println( "Long Value 1: " + long1);
double double2 = 99.6 ;
System.out.println( "Double Value 2: " + double2);
long long2 = ( long )Math.round(double2);
System.out.println( "Long Value 2: " + long2);
}
}
|
Output
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
class GFG {
public static void main(String[] args)
{
double doubleValue = 4512.412 ;
System.out.println( "Double Value: " + doubleValue);
String str = String.valueOf(doubleValue);
int index = str.indexOf( '.' );
str = str.substring( 0 , index == - 1 ? str.length()
: index);
long longValue = Long.parseLong(str);
System.out.println( "Long Value: " + longValue);
}
}
|
Output
Double Value: 4512.412
Long Value: 4512
Share your thoughts in the comments
Please Login to comment...