Hello! How to convert Double to String while retaining certain decimal places in KMP?
String.format
,
DecimalFormat
, and
BigDecimal
can only be used in the JVM
h
Hans van Dodewaard
05/24/2024, 11:12 AM
In common:
Copy code
expect fun Double.format(digits: Int):String
In js:
Copy code
actual fun Double.format(digits: Int) = this.asDynamic().toFixed(digits) as String
In Jvm:
Copy code
actual fun Double.format(digits: Int) =
"%.${digits}f".format(Locale.ENGLISH,this)
z
Zhang Zihan
05/24/2024, 11:42 AM
@Hans van Dodewaard Is there any method that can be used on Native?
h
Hans van Dodewaard
05/24/2024, 12:00 PM
I have no experience with that, but maybe something like
Copy code
actual fun Double.format(digits): String =
if (Platform.osFamily == OsFamily.ANDROID) "%.${digits}f".format(Locale.ENGLISH,this) else { // iOS implementation using NSNumberFormatter// ... }