The external Keyword
JavaScript has a number of internationalization APIs under the Intl object. You will be using the Intl.NumberFormat class, which can format currency for a given locale. You can find its documentation at developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat.
Try using this class in your Currency.kt file. Do not attempt to add any import statements. You will see errors after you make this change, which we will explain afterward.
Listing 26.9 Using an unknown JavaScript class (jsMain/kotlin/Currency.kt
)
actual fun Double.formatAsCurrency(): String {return toString()val numberFormatter = Intl.NumberFormat() return numberFormatter.format(this) }
Kotlin/JVM and ...
Get Kotlin Programming: The Big Nerd Ranch Guide, 2nd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.