toString

Common
JVM
JS
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.

Common
JVM
JS
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.