Question:
How to convert a double to long in java?
Answer:
1) Casting
2) Math.round()
3) Double.longValue()
2) Math.round()
3) Double.longValue()
Code:
public class Example { public static void main(String args[]){ double d1 = 1234.56; System.out.println("d1: " + d1); // Casting long l1 = (long)d1; // Rounding long l2 = Math.round(d1); System.out.println("l1: " + l1); System.out.println("l2: " + l2); // Double.longValue() Double d2 = new Double(d1); long l3 = d2.longValue(); System.out.println("l3: " + l3); } }
Output:
$ java Example d1: 1234.56 l1: 1234 l2: 1235 l3: 1234