What would be the equivalent of `java.text.NumberF...
# webassembly
j
What would be the equivalent of
java.text.NumberFormat
when using Wasm based Compose for Web?
e
I'm doing this to replace
java.text.DecimalFormat
. There's probably some API that could do something similar for
java.text.NumberFormat
👍 1
j
Call out to Intl, presumably
j
Call out to Intl, presumably
do you mean through something like
import js.intl.NumberFormat
? That works in Kotlin/JS but not wasm I think
j
That why I said "call out", as in, call into the JS runtime's JS API for Intl
j
Yeah. Define whatever subset of the API you need and you should be good.
👍 1
j
Works great, thanks
Copy code
private external object Intl {
    class NumberFormat(locales: String, options: JsAny) {
        fun format(l: Double): String
    }
}

private fun formatPercentageOptions(): JsAny = js("({ style: 'percent', maximumFractionDigits: 2 })")
private fun formatAsUSDOptions(): JsAny = js("({ style: 'currency', currency: 'USD',})")

internal actual fun Double.formatPercentage(): String {
    val format = Intl.NumberFormat(
        locales = "en-US",
        options = formatPercentageOptions(),
    )
    return format.format(this)
}

internal actual fun Double.formatDollarAmount(): String {
    val format = Intl.NumberFormat(
        locales = "en-US",
        options = formatAsUSDOptions(),
    )
    return format.format(this)
}