https://kotlinlang.org logo
#multiplatform
Title
# multiplatform
t

tylerwilson

01/08/2020, 2:57 AM
I am surprised I had not noticed this before, but I needed a String.format in common code (%.2d for example), and turns out it is only on JVM. Seems like some low hanging fruit. 🙂 Maybe for 1.4?
j

jw

01/08/2020, 3:02 AM
It is very much not low-hanging fruit as it requires extensive localization information and a locale mechanism.
if you just want two decimal places there are ample techniques to do it without string format. multiply by 100, cast to int, and insert the dot or just call toString and do string manipulation to truncate to 2 decimal places and/or fill in zeros
t

tylerwilson

01/08/2020, 3:13 AM
Right, I understand it is quite a bit more complicated than the API makes it seem. I will making a simple expect Platform.formatString() wrapper to try to handle it for now. Thanks.
m

msink

01/08/2020, 8:18 AM
I think here better fits some extension function like
Int.toString(width = 0, zeroFill = false, leftJustify = false)
👎 2
j

jw

01/08/2020, 2:29 PM
Except as soon as you want to display separators between orders of magnitude by the thousands you're back to the localization and locale problem.
m

msink

01/08/2020, 2:46 PM
Why? It can read all that from current locale. Anyway - I mean it should be done in separate extension functions, not in
printf
-stile.
t

tylerwilson

01/08/2020, 3:16 PM
I ended up creating an expect Platform.asPrice(value: Double): String function that uses the NumberFormat on Android and NSNumberFormatter on iOS. Working well so far. JS left for future…
⬆️ 1
j

jw

01/08/2020, 4:13 PM
What if I want to print it in the locale of the user I am sending the data to rather than the locale of the executing machine? How do I specify that in common code?
m

msink

01/08/2020, 4:36 PM
I'm not expert, but here it somehow works: https://github.com/icerockdev/moko-resources
t

tylerwilson

01/08/2020, 5:12 PM
It is ugly, but I usually have an initial config method called from the app to set up locale, settings location, etc. upon login, and then clear it on logout.
j

jw

01/08/2020, 8:31 PM
I don't see anything related to dynamically choosing a locale in that repo. It seems to rely on whatever the OS behavior is.
3 Views