toString

Common
JVM
JS
Native
1.0
fun toString(): String
(source)

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(Duration.days(45)) // 45.0d
println(Duration.days(1.5)) // 36.0h
println(Duration.minutes(1230)) // 20.5h
println(Duration.minutes(920)) // 920m
println(Duration.seconds(1.546)) // 1.55s
println(Duration.milliseconds(25.12)) // 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
(source)

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(Duration.minutes(1230).toString(DurationUnit.DAYS, 2)) // 0.85d
println(Duration.minutes(1230).toString(DurationUnit.HOURS, 2)) // 20.50h
println(Duration.minutes(1230).toString(DurationUnit.MINUTES)) // 1230m
println(Duration.minutes(1230).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.