hello, it might be trivial but... how to format ...
# multiplatform
r
hello, it might be trivial but... how to format a double to two decimal points like 1.123456 -> "1.12" so the decimal point is marked with
.
with pure kotlin? I can see that
.format
in kotlin stdlib uses java
java.lang.String.format
so it depends on the current locale whether it is
.
or
,
I want to be able to enforce it to use
.
.
r
thank you, that is very useful
l
Technically you could substring from 0 to indexOf('.')
r
@Landry Norris but in some languages it is "1.123" and some others it might be "1,123" and then 1.1 I want to be "1.100"
e
if you're using
String.format
on JVM, there is an overload with a
Locale
parameter
r
I know I know, but I want to have it in
commonMain
and use it later in iOS project too. So I was hoping there is pure kotlin function. Anyway for now I wrote a simple function which achieves the same with stringbuilder and bunch of multiplications and modulo operations.
e
there is no pure Kotlin format.
Double.toString()
does not depend on locale and always uses
.
(although its output does vary slightly on different platforms)