nanoseconds
Returns a Duration equal to this Int number of nanoseconds.
Since Kotlin
1.6Samples
import kotlin.test.*
import kotlin.time.*
import kotlin.time.Duration.Companion.days
import kotlin.time.Duration.Companion.hours
import kotlin.time.Duration.Companion.microseconds
import kotlin.time.Duration.Companion.milliseconds
import kotlin.time.Duration.Companion.minutes
import kotlin.time.Duration.Companion.nanoseconds
import kotlin.time.Duration.Companion.seconds
fun main() {
//sampleStart
println(15.nanoseconds) // 15ns
// Large values can be accurately represented only with millisecond precision
println(9000000000_054_775_807.nanoseconds) // 104166d 16h 0m 0.054s
// Fractional part is rounded to the nearest integer nanosecond value
println(999.4.nanoseconds) // 999ns
println(999.9.nanoseconds) // 1us
// Double.NaN.nanoseconds // will fail with IllegalArgumentException
//sampleEnd
}Returns a Duration equal to this Long number of nanoseconds.
Since Kotlin
1.6Samples
import kotlin.test.*
import kotlin.time.*
import kotlin.time.Duration.Companion.days
import kotlin.time.Duration.Companion.hours
import kotlin.time.Duration.Companion.microseconds
import kotlin.time.Duration.Companion.milliseconds
import kotlin.time.Duration.Companion.minutes
import kotlin.time.Duration.Companion.nanoseconds
import kotlin.time.Duration.Companion.seconds
fun main() {
//sampleStart
println(15.nanoseconds) // 15ns
// Large values can be accurately represented only with millisecond precision
println(9000000000_054_775_807.nanoseconds) // 104166d 16h 0m 0.054s
// Fractional part is rounded to the nearest integer nanosecond value
println(999.4.nanoseconds) // 999ns
println(999.9.nanoseconds) // 1us
// Double.NaN.nanoseconds // will fail with IllegalArgumentException
//sampleEnd
}Returns a Duration equal to this Double number of nanoseconds.
Depending on its magnitude, the value is rounded to an integer number of nanoseconds or milliseconds.
Since Kotlin
1.6Throws
if this Double value is NaN.
Samples
import kotlin.test.*
import kotlin.time.*
import kotlin.time.Duration.Companion.days
import kotlin.time.Duration.Companion.hours
import kotlin.time.Duration.Companion.microseconds
import kotlin.time.Duration.Companion.milliseconds
import kotlin.time.Duration.Companion.minutes
import kotlin.time.Duration.Companion.nanoseconds
import kotlin.time.Duration.Companion.seconds
fun main() {
//sampleStart
println(15.nanoseconds) // 15ns
// Large values can be accurately represented only with millisecond precision
println(9000000000_054_775_807.nanoseconds) // 104166d 16h 0m 0.054s
// Fractional part is rounded to the nearest integer nanosecond value
println(999.4.nanoseconds) // 999ns
println(999.9.nanoseconds) // 1us
// Double.NaN.nanoseconds // will fail with IllegalArgumentException
//sampleEnd
}