Updated on 2026-08-14

This commit is contained in:
Tangem 2026-05-06 22:27:01 +02:00
parent 13f139afa8
commit 80e694892d
2 changed files with 30 additions and 26 deletions

View file

@ -98,13 +98,22 @@ fun BigDecimalFiatFormatStyled.defaultAmount(spanStyleReference: SpanStyleRefere
}
val decimalSeparator = (formatter as? DecimalFormat)?.decimalFormatSymbols?.decimalSeparator
val formattedAmount = formatter.format(formattingAmount)
.replace(formatterCurrency.getSymbol(locale), fiatCurrencySymbol)
val currencySymbol = formatterCurrency.getSymbol(locale)
val rawFormatted = formatter.format(formattingAmount)
val separatorIndex = decimalSeparator?.let { formattedAmount.indexOf(it) } ?: formattedAmount.length
val separatorIndex = decimalSeparator?.let { rawFormatted.indexOf(it).takeIf { i -> i >= 0 } }
?: rawFormatted.length
val wholePart = formattedAmount.take(separatorIndex)
val fractionalPart = formattedAmount.drop(separatorIndex)
val formattedAmount = rawFormatted.replace(currencySymbol, fiatCurrencySymbol)
val offset = fiatCurrencySymbol.length - currencySymbol.length
val adjustedIndex = if (rawFormatted.indexOf(currencySymbol) in 0 until separatorIndex) {
separatorIndex + offset
} else {
separatorIndex
}
val wholePart = formattedAmount.take(adjustedIndex)
val fractionalPart = formattedAmount.drop(adjustedIndex)
combinedReference(
if (formattingAmount.isLessThanThreshold()) stringReference(CAN_BE_LOWER_SIGN) else TextReference.EMPTY,
@ -192,13 +201,22 @@ private fun BigDecimalFiatFormatStyled.price(spanStyleReference: SpanStyleRefere
}
val decimalSeparator = (formatter as? DecimalFormat)?.decimalFormatSymbols?.decimalSeparator
val formattedAmount = formatter.format(priceAmount)
.replace(formatterCurrency.getSymbol(locale), fiatCurrencySymbol)
val currencySymbol = formatterCurrency.getSymbol(locale)
val rawFormatted = formatter.format(priceAmount)
val separatorIndex = decimalSeparator?.let { formattedAmount.indexOf(it) } ?: formattedAmount.length
val separatorIndex = decimalSeparator?.let { rawFormatted.indexOf(it).takeIf { i -> i >= 0 } }
?: rawFormatted.length
val wholePart = formattedAmount.take(separatorIndex)
val fractionalPart = formattedAmount.drop(separatorIndex)
val formattedAmount = rawFormatted.replace(currencySymbol, fiatCurrencySymbol)
val offset = fiatCurrencySymbol.length - currencySymbol.length
val adjustedIndex = if (rawFormatted.indexOf(currencySymbol) in 0 until separatorIndex) {
separatorIndex + offset
} else {
separatorIndex
}
val wholePart = formattedAmount.take(adjustedIndex)
val fractionalPart = formattedAmount.drop(adjustedIndex)
combinedReference(
stringReference(wholePart),
@ -232,7 +250,7 @@ private fun BigDecimal.isLessThanThreshold() = this > BigDecimal.ZERO && this <
* Returns amount with correct scale
*/
fun getFiatPriceAmountWithScale(value: BigDecimal): Pair<BigDecimal, Int> {
return if (value > BigDecimal.ZERO && value < BigDecimal.ONE) {
return if (value < BigDecimal.ONE) {
val leadingZeroes = value.scale() - value.precision()
val scale = leadingZeroes + FRACTIONAL_PART_LENGTH_AFTER_LEADING_ZEROES

View file

@ -15,7 +15,6 @@ import com.tangem.domain.markets.TokenMarket
import com.tangem.features.feed.impl.R
import com.tangem.features.feed.model.market.list.state.MarketsListUM
import com.tangem.utils.converter.Converter
import com.tangem.utils.logging.TangemLogger
import kotlinx.collections.immutable.toImmutableList
import java.math.BigDecimal
import java.math.RoundingMode
@ -92,16 +91,7 @@ internal class MarketsTokenItemConverter(
private fun TokenMarket.getCurrentPrice(prev: TokenMarket? = null): MarketsListItemUM.Price {
val prevPrice = prev?.tokenQuotesShort?.currentPrice
val currentPrice = tokenQuotesShort.currentPrice
if (currentPrice < BigDecimal.ZERO) {
TangemLogger.withTag(MARKETS_PRICE_LOG_TAG).w(
messageString = "Unexpected non-positive price for tokenId=$id, symbol=$symbol, " +
"currency=${appCurrency.code}, value=${currentPrice.toPlainString()}",
)
}
val priceText = currentPrice.format {
val priceText = tokenQuotesShort.currentPrice.format {
fiat(
fiatCurrencyCode = appCurrency.code,
fiatCurrencySymbol = appCurrency.symbol,
@ -171,8 +161,4 @@ internal class MarketsTokenItemConverter(
return percent.format { percent() }
}
private companion object {
const val MARKETS_PRICE_LOG_TAG = "MarketsTokenItemConverter"
}
}