You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
63 lines
2.1 KiB
63 lines
2.1 KiB
package net.pokeranalytics.android.util
|
|
|
|
import android.content.Context
|
|
import net.pokeranalytics.android.model.realm.Bankroll
|
|
import java.text.NumberFormat
|
|
import java.util.*
|
|
|
|
class CurrencyUtils {
|
|
|
|
companion object {
|
|
|
|
/**
|
|
* return the currency associated with this bankroll
|
|
*/
|
|
fun getCurrency(bankroll: Bankroll? = null) : Currency {
|
|
val currencyCode = bankroll?.currency?.code ?: CurrencyUtils.getLocaleCurrency().currencyCode
|
|
return Currency.getInstance(currencyCode)
|
|
}
|
|
|
|
/**
|
|
* Get a currency formatter
|
|
*/
|
|
fun getCurrencyFormatter(context: Context, currency: Currency? = null) : NumberFormat {
|
|
val currencyFormatter = NumberFormat.getCurrencyInstance(Preferences.getCurrencyLocale(context))
|
|
currency?.let {
|
|
currencyFormatter.currency = it
|
|
}
|
|
currencyFormatter.minimumFractionDigits = 0
|
|
currencyFormatter.maximumFractionDigits = 2
|
|
return currencyFormatter
|
|
}
|
|
|
|
/**
|
|
* Get a currency rate formatter
|
|
*/
|
|
fun getCurrencyRateFormatter() : NumberFormat {
|
|
val currencyFormatter = NumberFormat.getInstance()
|
|
currencyFormatter.minimumFractionDigits = 0
|
|
currencyFormatter.maximumFractionDigits = 6
|
|
return currencyFormatter
|
|
}
|
|
|
|
/**
|
|
* Return the locale currency, or en_US if there
|
|
*/
|
|
fun getLocaleCurrency() : java.util.Currency {
|
|
return try {
|
|
java.util.Currency.getInstance(Locale.getDefault())
|
|
} catch (ex: Exception) {
|
|
when (Locale.getDefault().language) {
|
|
"en" -> java.util.Currency.getInstance(Locale("en", "US"))
|
|
"fr" -> java.util.Currency.getInstance(Locale("fr", "FR"))
|
|
"es" -> java.util.Currency.getInstance(Locale("es", "ES"))
|
|
"de" -> java.util.Currency.getInstance(Locale("de", "DE"))
|
|
"ja" -> java.util.Currency.getInstance(Locale("ja", "JP"))
|
|
"zh" -> java.util.Currency.getInstance(Locale("zh", "CN"))
|
|
else -> java.util.Currency.getInstance(Locale("en", "US"))
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
} |