Hey Guys, I have string which is coming from serve...
# android
v
Hey Guys, I have string which is coming from server.
Copy code
val string = "2.2"
I want to show in textview like
2.2
. But if my string is
2.00
it will show
2
in my textview. How can I do this in kotlin efficient way?
t
Two options: Format with kotlin or format with android strings.xml
If you plan on supporting multiple languages, consider using Android strings.xml
v
Ok
a
Convert it to double and use a
NumberFormat
for this. You can do something like this if you have a locale. Remember to check for empty and non numeric values in your string when converting to double.
Copy code
NumberFormat.getInstance(Locale("en", "US"))
        .format(yourStringInDouble).replace(".00", "")
t
Yep! Abinet’s is the way to go if you do it via Kotlin. Make sure to use the user device’s locale though, and not hardcode it to english.
v
@Tim Oltjenbruns what do you mean by
not harcode it to english
?
a
Don’t use
Locale("en", "US")
, use the Locale of the device
t
The above example uses
Locale("en", "US")
This hardcodes the number formatting to english
Yep! Beat me to it lol
😃 2
v
Ok got it thanks 😊
d
With locale set to device, locales using
,
for decimals would show
2,00
.