Using TimeUnit
, how can I convert 665477 nanosecond to 0.665477 millisecond?
long t = TimeUnit.MILLISECONDS.convert(665477L, TimeUnit.NANOSECONDS);
This always gives 0
but I need decimal points precision.
A nanosecond (ns) is a unit of time in the International System of Units (SI) equal to one billionth of a second, that is, 1⁄1 000 000 000 of a second, or 10−9 seconds.
There are 0.000001 milliseconds in a nanosecond.
How to Convert Nanoseconds to Milliseconds. To convert a nanosecond measurement to a millisecond measurement, divide the time by the conversion ratio. The time in milliseconds is equal to the nanoseconds divided by 1,000,000.
Nanosecond is one billionth of a second. Microsecond is one millionth of a second. Millisecond is one thousandth of a second.
shorter and less error prone:
double millis = 665477 / 1E6;
milli -> mikro -> nano
are two steps, each step has a conversion faktor of 1000 = 1E3; So makes one million, which can easier be read as 1E6, than by counting zeros.
From Java Documentation - TimeUnit#convert
public long convert(long sourceDuration,TimeUnit sourceUnit)
Convert the given time duration in the given unit to this unit. Conversions from finer to coarser granularities truncate, so lose precision. For example converting 999 milliseconds to seconds results in 0. Conversions from coarser to finer granularities with arguments that would numerically overflow saturate to Long.MIN_VALUE if negative or Long.MAX_VALUE if positive.
So to get your answer
double milliseconds = 665477 / 1000000.0;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With