Skip to content Skip to sidebar Skip to footer

Get Currency Symbol Of Only One Character (e.g $,₹, Etc) (locale Doesn't Matter) Android Kotlin

i have currency code (e.g. USD,INR,etc...). I want to get symbols of only one letter of those codes (e.g $,₹, etc). i have tried to find many solutions like this but it doesn't w

Solution 1:

try to calling default Locale in getSymbol() like getSymbol(Locale.getDefault(Locale.Category.DISPLAY)) check below code

Currencypound= Currency.getInstance("GBP");
pound.getSymbol(Locale.getDefault(Locale.Category.DISPLAY));

Solution 2:

This works fine for me

import android.os.Build

import android.support.v7.app.AppCompatActivity
import android.os.Bundle

import kotlinx.android.synthetic.main.activity_main.*
import java.util.*

classMainActivity : AppCompatActivity() {

    overridefunonCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        count()
    }

    funcount() {
        val pound = Currency.getInstance("USD")
        var str:String
        str = if(Build.VERSION.SDK_INT >=24) pound.getSymbol(Locale.getDefault(Locale.Category.DISPLAY))
        else pound.getSymbol(resources.configuration.locale)
        tvText.text = str
    }


}

Solution 3:

I simply created this kotlin extension function which isolates the currency symbol if there is one or uses the provided symbol if there isn't one.

// Supply the Currency Symbol, e.g. US$, would return $ and IDR would return IDR
fun String.isolateCurrencySymbol(): String{
    for (char in this){
        if (char !in CharRange('A', 'Z')){
            returnchar.toString()
        }
    }
    returnthis
}

Post a Comment for "Get Currency Symbol Of Only One Character (e.g $,₹, Etc) (locale Doesn't Matter) Android Kotlin"