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:
"0s"
"Infinity"
without unitimport 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
.
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:
"Infinity"
without unitimport 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
}
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