Spec-Zone.ru › Kotlin 2

milliseconds

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

Начиная с Kotlin: 1.6

val Int.milliseconds: 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(1500.milliseconds) // 1.5s

// Large values can be converted to an infinite duration
println(9_000_000_000_000_000_807.milliseconds) // Infinity

// Fractional part is rounded to the nearest integer nanosecond value
println(2.000_000_4.milliseconds) // 2ms
println(2.000_000_9.milliseconds) // 2.000001ms
// Or to the nearest integer millisecond value
println(9_000_000_000_000_807.5.milliseconds) // 104166666d 16h 0m 0.808s

// Double.NaN.milliseconds // will fail with IllegalArgumentException 
   //sampleEnd
}

val Long.milliseconds: 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(1500.milliseconds) // 1.5s

// Large values can be converted to an infinite duration
println(9_000_000_000_000_000_807.milliseconds) // Infinity

// Fractional part is rounded to the nearest integer nanosecond value
println(2.000_000_4.milliseconds) // 2ms
println(2.000_000_9.milliseconds) // 2.000001ms
// Or to the nearest integer millisecond value
println(9_000_000_000_000_807.5.milliseconds) // 104166666d 16h 0m 0.808s

// Double.NaN.milliseconds // will fail with IllegalArgumentException 
   //sampleEnd
}

val Double.milliseconds: 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(1500.milliseconds) // 1.5s

// Large values can be converted to an infinite duration
println(9_000_000_000_000_000_807.milliseconds) // Infinity

// Fractional part is rounded to the nearest integer nanosecond value
println(2.000_000_4.milliseconds) // 2ms
println(2.000_000_9.milliseconds) // 2.000001ms
// Or to the nearest integer millisecond value
println(9_000_000_000_000_807.5.milliseconds) // 104166666d 16h 0m 0.808s

// Double.NaN.milliseconds // 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/milliseconds.html

Spec-Zone.ru

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