Skip to content

Commit

Permalink
Merge branch 'shorten-big-prices'
Browse files Browse the repository at this point in the history
  • Loading branch information
shorthouse committed Apr 2, 2024
2 parents cd96f17 + 9189700 commit 26e0a83
Showing 1 changed file with 11 additions and 11 deletions.
22 changes: 11 additions & 11 deletions app/src/main/java/dev/shorthouse/coinwatch/model/Price.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,28 @@ import java.util.Currency as CurrencyCode
data class Price(val price: String?, val currency: Currency = Currency.USD) : Comparable<Price> {
val amount: BigDecimal = price.toSanitisedBigDecimalOrZero()

private val currencyFormat by lazy {
val currencyFormat = DecimalFormat.getCurrencyInstance(Locale.US) as DecimalFormat
private val currencyFormat: DecimalFormat = getCurrencyFormat()

val formattedAmount: String = when {
price.isNullOrBlank() -> "${currency.symbol}--"
amount in belowOneThreshold -> currencyFormat.format(amount)
amount in smallThreshold -> currencyFormat.format(amount)
else -> formatLargeAmount()
}

private fun getCurrencyFormat(): DecimalFormat {
val currencyFormat = DecimalFormat.getCurrencyInstance(Locale.US) as DecimalFormat
val decimalPlaces = if (amount in belowOneThreshold) 6 else 2
val currencyCode = try {
CurrencyCode.getInstance(currency.name)
} catch (e: IllegalArgumentException) {
CurrencyCode.getInstance(Currency.USD.name)
}

currencyFormat.minimumFractionDigits = decimalPlaces
currencyFormat.maximumFractionDigits = decimalPlaces
currencyFormat.currency = currencyCode

currencyFormat
}

val formattedAmount: String = when {
price.isNullOrBlank() -> "${currency.symbol}--"
amount in belowOneThreshold -> currencyFormat.format(amount)
amount in smallThreshold -> currencyFormat.format(amount)
else -> formatLargeAmount()
return currencyFormat
}

private fun formatLargeAmount(): String {
Expand Down

0 comments on commit 26e0a83

Please sign in to comment.