Implement and test default string formatting in JVM and JS

Relax Duration.toString tests for JS and Native rounding specifics
This commit is contained in:
Ilya Gorbunov
2019-04-09 05:16:30 +03:00
parent 804d2b2d8c
commit b816f50d4d
4 changed files with 157 additions and 15 deletions
@@ -5,6 +5,24 @@
package kotlin.time
internal actual fun formatToDecimals(value: Double, decimals: Int, unitName: String): String {
TODO("not implemented")
import kotlin.js.json
import kotlin.math.*
internal actual fun formatToExactDecimals(value: Double, decimals: Int): String {
val rounded = if (decimals == 0) {
value
} else {
val pow = 10.0.pow(decimals)
@Suppress("DEPRECATION", "DEPRECATION_ERROR")
kotlin.js.Math.round(abs(value) * pow) / pow * sign(value)
}
return rounded.asDynamic().toFixed(decimals).unsafeCast<String>()
}
internal actual fun formatUpToDecimals(value: Double, decimals: Int): String {
return value.asDynamic().toLocaleString("en-us", json("maximumFractionDigits" to decimals)).unsafeCast<String>()
}
internal actual fun formatScientific(value: Double): String {
return value.asDynamic().toExponential(2).unsafeCast<String>().replace("+", "")
}