Spec-Zone.ru › Kotlin 2

nanoseconds

kotlin-stdlib/kotlin.time/Duration/Companion/nanoseconds

Начиная с Kotlin: 1.6

val Int.nanoseconds: Duration

Возвращает Duration, равный этому количеству наносекунд типа Int.

Примеры

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
}

val Long.nanoseconds: Duration

Возвращает Duration, равный этому количеству наносекунд типа Long.

Примеры

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
}

val Double.nanoseconds: Duration

Возвращает Duration, равный этому количеству наносекунд типа Double.

В зависимости от величины значение округляется до целого количества наносекунд или миллисекунд.

Выбрасывает

IllegalArgumentException

если это значение типа Double является NaN.

Примеры

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
}

© 2010–2026 JetBrains s.r.o. and Kotlin Programming Language contributors
Licensed under the Apache License, Version 2.0.
https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.time/-duration/-companion/nanoseconds.html

Spec-Zone.ru

Настройки Оффлайн Что нового Помощь О нас
Spec-Zone .ru
спецификации, руководства, описания, API