Change Duration.toString scientific exponent formatting
- use 'e+' for positive exponents in scientific format
This commit is contained in:
@@ -24,5 +24,5 @@ internal actual fun formatUpToDecimals(value: Double, decimals: Int): String {
|
||||
}
|
||||
|
||||
internal actual fun formatScientific(value: Double): String {
|
||||
return value.asDynamic().toExponential(2).unsafeCast<String>().replace("+", "")
|
||||
return value.asDynamic().toExponential(2).unsafeCast<String>()
|
||||
}
|
||||
@@ -11,10 +11,11 @@ import java.text.DecimalFormatSymbols
|
||||
import java.util.*
|
||||
import kotlin.concurrent.getOrSet
|
||||
|
||||
private val rootFormatSymbols = DecimalFormatSymbols(Locale.ROOT).apply { exponentSeparator = "e" }
|
||||
private val rootNegativeExpFormatSymbols = DecimalFormatSymbols(Locale.ROOT).apply { exponentSeparator = "e" }
|
||||
private val rootPositiveExpFormatSymbols = DecimalFormatSymbols(Locale.ROOT).apply { exponentSeparator = "e+" }
|
||||
private val precisionFormats = Array(4) { ThreadLocal<DecimalFormat>() }
|
||||
|
||||
private fun createFormatForDecimals(decimals: Int) = DecimalFormat("0", rootFormatSymbols).apply {
|
||||
private fun createFormatForDecimals(decimals: Int) = DecimalFormat("0", rootNegativeExpFormatSymbols).apply {
|
||||
if (decimals > 0) minimumFractionDigits = decimals
|
||||
roundingMode = RoundingMode.HALF_UP
|
||||
}
|
||||
@@ -34,4 +35,8 @@ internal actual fun formatUpToDecimals(value: Double, decimals: Int): String =
|
||||
|
||||
private val scientificFormat = ThreadLocal<DecimalFormat>()
|
||||
internal actual fun formatScientific(value: Double): String =
|
||||
scientificFormat.getOrSet { DecimalFormat("0E0", rootFormatSymbols).apply { minimumFractionDigits = 2 } }.format(value)
|
||||
scientificFormat.getOrSet { DecimalFormat("0E0", rootNegativeExpFormatSymbols).apply { minimumFractionDigits = 2 } }
|
||||
.apply {
|
||||
decimalFormatSymbols = if (value >= 1 || value <= -1) rootPositiveExpFormatSymbols else rootNegativeExpFormatSymbols
|
||||
}
|
||||
.format(value)
|
||||
@@ -309,12 +309,12 @@ class DurationTest {
|
||||
test(DurationUnit.NANOSECONDS, "0ns", "0.0ns", "0.02ns", "0.020ns", "0.0202ns")
|
||||
|
||||
d = Double.MAX_VALUE.nanoseconds
|
||||
test(DurationUnit.DAYS, "2.08e294d")
|
||||
test(DurationUnit.NANOSECONDS, "1.80e308ns")
|
||||
test(DurationUnit.DAYS, "2.08e+294d")
|
||||
test(DurationUnit.NANOSECONDS, "1.80e+308ns")
|
||||
|
||||
assertEquals("0.500000000000s", 0.5.seconds.toString(DurationUnit.SECONDS, 100))
|
||||
assertEquals("99999000000000.000000000000ns", 99_999.seconds.toString(DurationUnit.NANOSECONDS, 15))
|
||||
assertEquals("1.00e14ns", 100_000.seconds.toString(DurationUnit.NANOSECONDS, 9))
|
||||
assertEquals("1.00e+14ns", 100_000.seconds.toString(DurationUnit.NANOSECONDS, 9))
|
||||
|
||||
d = Duration.INFINITE
|
||||
test(DurationUnit.DAYS, "Infinity", "Infinity")
|
||||
@@ -378,15 +378,15 @@ class DurationTest {
|
||||
|
||||
test(Duration.ZERO, "0s")
|
||||
test(365.days * 10000, "3650000d")
|
||||
test(300.days * 100000, "3e7d", "3.00e7d")
|
||||
test(365.days * 100000, "3.65e7d")
|
||||
test(300.days * 100000, "3.00e+7d")
|
||||
test(365.days * 100000, "3.65e+7d")
|
||||
|
||||
val universeAge = 365.25.days * 13.799e9
|
||||
val planckTime = 5.4e-44.seconds
|
||||
|
||||
test(universeAge, "5.04e12d")
|
||||
test(planckTime, "5.4e-44s", "5.40e-44s")
|
||||
test(Double.MAX_VALUE.nanoseconds, "2.08e294d")
|
||||
test(universeAge, "5.04e+12d")
|
||||
test(planckTime, "5.40e-44s")
|
||||
test(Double.MAX_VALUE.nanoseconds, "2.08e+294d")
|
||||
test(Duration.INFINITE, "Infinity")
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user