toString

open override 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

Return

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

Samples

import samples.*

import kotlin.time.*
fun main() { 
   //sampleStart 
   assertPrints(Duration.days(45), "45.0d")
assertPrints(Duration.days(1.5), "36.0h")
assertPrints(Duration.minutes(1230), "20.5h")
assertPrints(Duration.minutes(920), "920m")
assertPrints(Duration.seconds(1.546), "1.55s")
assertPrints(Duration.milliseconds(25.12), "25.1ms") 
   //sampleEnd
}

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

Return

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

Samples

import samples.*

import kotlin.time.*
fun main() { 
   //sampleStart 
   assertPrints(Duration.minutes(1230).toString(DurationUnit.DAYS, 2), "0.85d")
assertPrints(Duration.minutes(1230).toString(DurationUnit.HOURS, 2), "20.50h")
assertPrints(Duration.minutes(1230).toString(DurationUnit.MINUTES), "1230m")
assertPrints(Duration.minutes(1230).toString(DurationUnit.SECONDS), "73800s") 
   //sampleEnd
}

Throws