java double to long

htt‮p‬s://www.lautturi.com
java double to long

To convert a double value to a long in Java, you can use the longValue() method of the Double class or use a type cast.

Here's an example of how to use the longValue() method:

double d = 123.45;
long l = d.longValue();  // l is now 123

This will truncate the decimal part of the double value and convert it to a long.

Alternatively, you can use a type cast to convert the double to a long like this:

double d = 123.45;
long l = (long) d;  // l is now 123

It's important to note that using a type cast will also truncate the decimal part of the double value and may result in a loss of precision.

You can use either of these approaches to convert a double to a long in Java.

Created Time:2017-11-03 15:57:11  Author:lautturi