Duration: round Double value to Long ns instead of truncating it KT-47675
This commit is contained in:
@@ -9,6 +9,7 @@ import kotlin.contracts.*
|
||||
import kotlin.jvm.JvmInline
|
||||
import kotlin.math.abs
|
||||
import kotlin.math.roundToInt
|
||||
import kotlin.math.roundToLong
|
||||
import kotlin.math.sign
|
||||
|
||||
/**
|
||||
@@ -816,11 +817,11 @@ public fun Long.toDuration(unit: DurationUnit): Duration {
|
||||
public fun Double.toDuration(unit: DurationUnit): Duration {
|
||||
val valueInNs = convertDurationUnit(this, unit, DurationUnit.NANOSECONDS)
|
||||
require(!valueInNs.isNaN()) { "Duration value cannot be NaN." }
|
||||
val nanos = valueInNs.toLong()
|
||||
val nanos = valueInNs.roundToLong()
|
||||
return if (nanos in -MAX_NANOS..MAX_NANOS) {
|
||||
durationOfNanos(nanos)
|
||||
} else {
|
||||
val millis = convertDurationUnit(this, unit, DurationUnit.MILLISECONDS).toLong()
|
||||
val millis = convertDurationUnit(this, unit, DurationUnit.MILLISECONDS).roundToLong()
|
||||
durationOfMillisNormalized(millis)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user