Fran
Fran

Reputation: 554

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?

Upvotes: 0

Views: 2311

Answers (3)

android_dev71
android_dev71

Reputation: 617

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: €

Upvotes: 1

Shivanand Darur
Shivanand Darur

Reputation: 3224

For Android, it could be an option.

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

Upvotes: 0

You Qi
You Qi

Reputation: 9211

you can try:

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

// symbol = £

Upvotes: 7

Related Questions