@@ -22,7 +22,6 @@ import to.bitkit.models.WidgetType
2222import to.bitkit.utils.AppError
2323import to.bitkit.utils.Logger
2424import java.text.NumberFormat
25- import java.util.Currency
2625import java.util.Locale
2726import javax.inject.Inject
2827import javax.inject.Singleton
@@ -147,24 +146,15 @@ class PriceService @Inject constructor(
147146 )
148147 }
149148
150- private fun formatPrice (pair : TradingPair , price : Double ): String {
149+ private fun formatPrice (
150+ pair : TradingPair ,
151+ price : Double ,
152+ locale : Locale = Locale .getDefault(),
153+ ): String {
151154 return runCatching {
152- val currency = Currency .getInstance(pair.quote)
153- val numberFormat = NumberFormat .getCurrencyInstance(Locale .US ).apply {
154- this .currency = currency
155- maximumFractionDigits = when {
156- price >= 1000 -> 0
157- price >= 1 -> 2
158- else -> 6
159- }
160- }
161-
162- // Format and remove currency symbol, keeping only the number with formatting
163- val formatted = numberFormat.format(price)
164- val currencySymbol = currency.symbol
165- formatted.replace(currencySymbol, " " ).trim()
155+ formatPriceValue(price = price, locale = locale)
166156 }.onFailure {
167- Logger .warn(" Error formatting price for ${pair.displayName} " , e = it, context = TAG )
157+ Logger .warn(" Failed to format price for ' ${pair.displayName} ' " , it, context = TAG )
168158 }.getOrDefault(String .format(Locale .US , " %.2f" , price))
169159 }
170160
@@ -180,3 +170,25 @@ sealed class PriceError(message: String) : AppError(message) {
180170 class InvalidResponse (override val message : String ) : PriceError(message)
181171 class NetworkError (override val message : String ) : PriceError(message)
182172}
173+
174+ private const val GROUPED_PRICE_THRESHOLD = 1_000.0
175+ private const val STANDARD_PRICE_THRESHOLD = 1.0
176+ private const val GROUPED_PRICE_DECIMALS = 0
177+ private const val STANDARD_PRICE_DECIMALS = 2
178+ private const val SMALL_PRICE_DECIMALS = 6
179+ private const val MIN_PRICE_DECIMALS = 0
180+
181+ internal fun formatPriceValue (
182+ price : Double ,
183+ locale : Locale = Locale .getDefault(),
184+ ): String {
185+ return NumberFormat .getNumberInstance(locale).apply {
186+ maximumFractionDigits = when {
187+ price >= GROUPED_PRICE_THRESHOLD -> GROUPED_PRICE_DECIMALS
188+ price >= STANDARD_PRICE_THRESHOLD -> STANDARD_PRICE_DECIMALS
189+ else -> SMALL_PRICE_DECIMALS
190+ }
191+ minimumFractionDigits = MIN_PRICE_DECIMALS
192+ isGroupingUsed = true
193+ }.format(price)
194+ }
0 commit comments