Hello! How to convert Double to String while retai...
# multiplatform
z
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
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
@Hans van Dodewaard Is there any method that can be used on Native?
h
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// ... }
z
Thanks for your help❣️