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:
Ilya Gorbunov
2019-06-17 04:46:24 +03:00
parent 5cf7c7e5bf
commit a5b7c270ae
3 changed files with 13 additions and 8 deletions
+5 -1
View File
@@ -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()
}