'I just need the local currency symbol in a String depending on the country the user is, in Kotlin for Android Studio

Is there a simple way for the system to return the local currency symbol?

I been searching but I cant find anything, in swift you can achieve this with two lines:

let locale = Locale.current

let currencySymbol = locale.currencySymbol!

¿Is there anything similar in Kotlin?



Solution 1:[1]

you can try:

val numberFormat = NumberFormat.getCurrencyInstance(Locale.getDefault())
val symbol = numberFormat.currency?.symbol

// symbol = £

Solution 2:[2]

For Android, it could be an option.

Currency.getInstance(Locale.getDefault()).currencyCode

Solution 3:[3]

In case useful, I paste my solution in Kotlin, for currency symbol (i.e. €) and currency code (i.e.: EUR) :

val locale = Locale.getDefault()
val numberFormat = NumberFormat.getCurrencyInstance(locale)
println(numberFormat.currency)  // on my device in Italy prints: EUR

val symbol = numberFormat.currency?.symbol
println(symbol) // on my device in Italy prints: €

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 You Qi
Solution 2
Solution 3