Change Duration.toString(unit, decimals) formatting
- switch to scientific notation when value is too big (greater than 1e14) - allow at most 12 decimals, larger values are coerced to 12 - use scientific format with exactly two decimals in mantissa in JVM
This commit is contained in:
@@ -279,7 +279,11 @@ public inline class Duration internal constructor(internal val value: Double) :
|
||||
public fun toString(unit: DurationUnit, decimals: Int = 0): String {
|
||||
require(decimals >= 0) { "decimals must be not negative, but was $decimals" }
|
||||
if (isInfinite()) return value.toString()
|
||||
return formatToExactDecimals(toDouble(unit), decimals) + unit.shortName()
|
||||
val number = toDouble(unit)
|
||||
return when {
|
||||
abs(number) < 1e14 -> formatToExactDecimals(number, decimals.coerceAtMost(12))
|
||||
else -> formatScientific(number)
|
||||
} + unit.shortName()
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user