How to convert double to long in Java

We often need to convert a floating point number into integral number e.g. a double or float value 234.50d to long value 234L or 235L. There are couple of ways to convert a double value to long value in Java e.g. you can simply cast a double value to long or you can wrap a double value into Double object and call it’s longValue() method, or using Math.round() method to round floating point value into nearest integer. Te right way to covert a double value to a long in Java really depends upon what you want to do with floating point value. If you just want to truncate the double value to remove zero and take integer value, you can simply cast double to long. If you have Double object instead of double primitive type then you can also Double.longValue() method, this doesn’t do anything but just cast the double primitive wrapped inside Double object to long. It’s clear from following code snippet taken from java.lang.Double class

 public long longValue() {
        return (long)value;

On the other hand if you want to round the double value to nearest long, you can use Math.round() method, this will return a long value rounded up to nearest position, for example if double value is 100.5 then it will rounded to 101, while if it is less than that e.g. 100.1 then it will be rounded to just 100. In next section we will see detailed examples of these 3 ways to convert double to long in Java.

3 Examples to Convert Double to Long in Java

Here are three examples of converting a floating point double value to long in Java. In our first example, we are using Double.longValue() to convert a double to long in Java. Here is the code :

double d = 129.00;
long l = (new Double(d)).longValue(); //129

well here we have first converted a double primitive to a Double object and then called longValue() method on that. Apart from this example, I would not do that because longValue() does nothing but just cast the double value to long, so if you have a primitive double, just directly cast it to long. Use this method only if you are getting a Double wrapper object.  Here is the code example of casting a double to long in Java, you will notice in both cases result is same.

double d = 129.00;
long v = (long) d; //129

How to convert Double to Long value in Java with ExampleCasting double to long just truncate trailing zero. This will be faster than going via the wrapper classes – and more importantly, it’s more readable. Now, if you need rounding  you can use Math.round() method to round a floating point number into nearest integral number

double d1 = 129.5;
long v1 = Math.round(d1); //130
double d2 = 129.2;
long v2 = Math.round(d2); //129

It doesn’t give the same result as a cast. So it depends on double value, if decimal point is 0.5 or greater than it will be rounded to next long value other wise to previous or lower long value. One more advantage of this method is that it works for wrapper class Long and Double as well. You can use any of these method to convert double to long in Java.

Double to Long Conversion in Java

Here is a complete example of converting a floating point double value to an integral long value in Java. In this one program, we have used all three ways explained in above paragraph. You can use this sample program to quickly run and check how it works. If you are using Eclipse IDE, just copy paste the code and it will automatically create correct Java source file and packages, provided you keep your mouse on a Java project.

 * Java Program to show how to convert a floating point double
 * value to a long in Java.
 * @author WINDOWS 8

public class DoubleToLong {
    public static void main(String args[]){
        // first example - converting double to long using longValue() method        
        double d = 102.9520;
        long l = (new Double(d)).longValue();
        System.out.println("double value=" + d + ", long=" + l);
        // second example - rather simple just cast double to long
        double bill = 293.05;
        long myBill = (long) bill;
        System.out.println("double value=" + bill + ", long=" + myBill);
        // third example - rounding double value to long in Java
        double dbl = 3421.56;
        long rnd = Math.round(dbl);
        System.out.println("double value=" + dbl + ", long=" + rnd);

double value=102.952, long=102
double value=293.05, long=293
double value=3421.56, long=3422

That’s all on how to convert a floating point double value into long in Java. As I said, it’s very easy to convert one data type to another in Java and double and long is no different. In my opinion, you should just cast a double value to long if you are not interested in decimal value or just want to truncate the decimal part of double value. Instead of double primitive if you get Double object, then simply call Double.longValue() method, this will do the same i.e. casting. On the other hand, if you are interested in fraction values and want to round the floating point value into nearest long then use Math.round() method, it will round up the double value into long.

Other Data Type conversion articles from Java67 blog

  • How to convert Array to String in Java (read here)
  • How to convert util date to sql date in JDBC (see here)
  • Best way to convert Numbers to String in Java (read here)
  • How to convert Enum to String in Java (see here)
  • How to convert String to Integer in Java (read here)
  • How to convert decimal to binary number in Java (see here)
  • Converting List to Set in Java (check here)
  • How to convert hexadecimal to decimal, binary, and octal in Java (see here)

Source: How to convert double to long in Java

Post author

Dustin Gurley is an Designer, Developer, Artist, Instructor, Critical Theorist and Systems Engineer. He has an extensive background working professionally with 2D/2.5D/3D Motion Graphics, Compositing, Film, Video, Photography and client-side performance techniques as it pertains to web development. Dustin recently completed work on his Master of Fine Art degree in Motion Media Design (Motion Graphics) from the Savannah College of Art and Design. Prior to beginning his graduate work, Dustin obtained a Bachelor of Art degree in Communication Studies with a concentration in Broadcast and Emerging Media from the University of North Carolina at Wilmington. In addition to design and modeling, Dustin enjoys toying with his view camera, working with scratch film, authoring media related material and contributing to various industry conferences. When not in front of a computer, Dustin can be found with his wife, Regina Everett Gurley. The couple enjoys dividing their time between their home just outside of Raleigh, North Carolina and the beautiful North Carolina coast. Currently, Dustin serves as the Lead Instructor of Internet Technologies for Wake Technical Community College in Raleigh, North Carolina.