W3cubDocs

/Kotlin

toString

Platform and version requirements: JVM (1.0), JS (1.0), Native (1.0)
fun toString(): String

Returns a string representation of this duration value expressed in the unit which yields the most compact and readable number value.

Special cases:

  • zero duration is formatted as "0s"
  • the infinite duration is formatted as "Infinity" without unit
  • very small durations (less than 1e-15 s) are expressed in seconds and formatted in scientific notation
  • very big durations (more than 1e+7 days) are expressed in days and formatted in scientific notation
import kotlin.time.*

fun main(args: Array<String>) {
//sampleStart
println(45.days) // 45.0d
println(1.5.days) // 36.0h
println(1230.minutes) // 20.5h
println(920.minutes) // 920m
println(1.546.seconds) // 1.55s
println(25.12.milliseconds) // 25.1ms
//sampleEnd
}

Return the value of duration in the automatically determined unit followed by that unit abbreviated name: d, h, m, s, ms, us, or ns.

Platform and version requirements: JVM (1.0), JS (1.0), Native (1.0)
fun toString(unit: DurationUnit, decimals: Int = 0): String

Returns a string representation of this duration value expressed in the given unit and formatted with the specified decimals number of digits after decimal point.

Special cases:

  • the infinite duration is formatted as "Infinity" without unit
import kotlin.time.*

fun main(args: Array<String>) {
//sampleStart
println(1230.minutes.toString(DurationUnit.DAYS, 2)) // 0.85d
println(1230.minutes.toString(DurationUnit.HOURS, 2)) // 20.50h
println(1230.minutes.toString(DurationUnit.MINUTES)) // 1230m
println(1230.minutes.toString(DurationUnit.SECONDS)) // 73800s
//sampleEnd
}

Exceptions

IllegalArgumentException - if decimals is less than zero.

Return the value of duration in the specified unit followed by that unit abbreviated name: d, h, m, s, ms, us, or ns.

© 2010–2020 JetBrains s.r.o. and Kotlin Programming Language contributors
Licensed under the Apache License, Version 2.0.
https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.time/-duration/to-string.html