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:
@@ -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("+", "")
|
||||
}
|
||||
Reference in New Issue
Block a user