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.