How to Convert a Double to Long in Java - Example Tutorial

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 a 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 a Double object and call it's longValue() method, or using Math.round() method to round floating point value to a nearest integer. Te right way to convert a double value to a long in Java really depends on upon what you want to do with the floating point value. If you just want to truncate the double value to remove zero and take an integer value, you can simply cast double to long. If you have Double object instead of the double primitive type then you can also Double.longValue() method, this doesn't do anything but just cast the double primitive wrapped inside the 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);
         
    }
   
}

Output
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)

4 comments:

  1. Since it Double you can also provide input in exponential format, for example

    long input = new Double("2.9158614E7").longValue();
    System.out.println(input);

    It will work and return 29158614, cheers !!

    ReplyDelete
  2. My money is on valueOf() method becasue that cache some values and if you are dealing in lower ranges, you will see a performance boost.

    ReplyDelete
  3. Converting double to long is nothing but casting double value to long e.g.

    double d = (long) l;

    ReplyDelete

  4. public class A {
    public static void main (String args[]) {
    String val = "20.30";
    Float fAmount = 100* Float.valueOf(val);
    long amount = fAmount.longValue();
    System.out.println("Amount in Rp.:"+amount);
    }
    }

    Console Ouptut:

    Amount in Rp.:2029



    The Float value is: 2029.9999 !

    ReplyDelete