Wednesday, December 11, 2019

How To Convert A Double To Long Inwards Coffee - Example Tutorial

We oft require to convert a floating signal publish into integral publish e.g. a double or float value 234.50d to long value 234L or 235L. There are a dyad of ways to convert a double value to long value inwards Java e.g. yous tin precisely cast a double value to long or yous tin wind a double value into a Double object together with telephone phone it's longValue() method, or using Math.round() method to circular floating signal value to a nearest integer. Te right means to convert a double value to a long inwards Java actually depends on upon what yous desire to exercise alongside the floating signal value. If yous precisely desire to truncate the double value to take away null together with accept an integer value, yous tin precisely cast double to long. If yous possess got Double object instead of the double primitive type together with thence yous tin besides Double.longValue() method, this doesn't exercise anything but precisely cast the double primitive wrapped within the Double object to long.

It's clear from next code snippet taken from java.lang.Double class

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

On the other manus if yous desire to circular the double value to nearest long, yous tin exercise Math.round() method, this volition render a long value rounded upward to nearest position, for instance if double value is 100.5 together with thence it volition rounded to 101, land if it is less than that e.g. 100.1 together with thence it volition last rounded to precisely 100. In side yesteryear side department nosotros volition run across detailed examples of these 3 ways to convert double to long inwards Java.




3 Examples to Convert Double to Long inwards Java

Here are 3 examples of converting a floating signal double value to long inwards Java. In our kickoff example, nosotros are using Double.longValue() to convert a double to long inwards Java. Here is the code :

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

good hither nosotros possess got kickoff converted a double primitive to a Double object together with and thence called longValue() method on that. Apart from this example, I would non exercise that because longValue() does nil but precisely cast the double value to long, thence if yous possess got a primitive double, precisely direct cast it to long. Use this method solely if yous are getting a Double wrapper object.  Here is the code instance of casting a double to long inwards Java, yous volition disclose inwards both cases lawsuit is same.

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

 We oft require to convert a floating signal publish into integral publish e How to Convert a Double to Long inwards Java - Example TutorialCasting double to long precisely truncate trailing zero. This volition last faster than going via the wrapper classes - together with to a greater extent than importantly, it's to a greater extent than readable. Now, if yous require rounding  you tin exercise Math.round() method to circular a floating signal publish 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 lawsuit every bit a cast. So it depends on double value, if decimal signal is 0.5 or greater than it volition last rounded to side yesteryear side long value other wise to previous or lower long value. One to a greater extent than payoff of this method is that it industrial plant for wrapper degree Long together with Double every bit well. You tin exercise whatsoever of these method to convert double to long inwards Java.


Double to Long Conversion inwards Java

Here is a consummate instance of converting a floating signal double value to an integral long value inwards Java. In this 1 program, nosotros possess got used all 3 ways explained inwards to a higher house paragraph. You tin exercise this sample plan to speedily run together with banking concern fit how it works. If yous are using Eclipse IDE, precisely re-create glue the code together with it volition automatically exercise right Java rootage file together with packages, provided yous continue your mouse on a Java project.

/**  * Java Program to demo how to convert a floating signal double  * value to a long inwards Java.  *   * @author WINDOWS 8  */  public class DoubleToLong {              public static void main(String args[]){                  // kickoff instance - 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);                  // minute instance - rather elementary precisely cast double to long         double mouth = 293.05;         long myBill = (long) bill;         System.out.println("double value=" + mouth + ", long=" + myBill);                                  // 3rd instance - rounding double value to long inwards 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 signal double value into long inwards Java. As I said, it's really slow to convert 1 information type to unopen to other inwards Java together with double together with long is no different. In my opinion, yous should precisely cast a double value to long if yous are non interested inwards decimal value or precisely desire to truncate the decimal locomote of double value. Instead of double primitive if yous larn Double object, together with thence precisely telephone phone Double.longValue() method, this volition exercise the same i.e. casting. On the other hand, if yous are interested inwards fraction values together with desire to circular the floating signal value into nearest long together with thence exercise Math.round() method, it volition circular upward the double value into long.

Further Learning
Data Structures together with Algorithms: Deep Dive Using Java
read here)
  • How to convert util appointment to sql appointment inwards JDBC (see here)
  • Best means to convert Numbers to String inwards Java (read here)
  • How to convert Enum to String inwards Java (see here)
  • How to convert String to Integer inwards Java (read here)
  • How to convert decimal to binary publish inwards Java (see here)
  • Converting List to Set inwards Java (check here)
  • How to convert hexadecimal to decimal, binary, together with octal inwards Java (see here)
  • No comments:

    Post a Comment