Skip to content

Commit f0a35f1

Browse files
committed
fix: remove price widget duplicate symbol
1 parent 3374c93 commit f0a35f1

2 files changed

Lines changed: 60 additions & 17 deletions

File tree

app/src/main/java/to/bitkit/data/widgets/PriceService.kt

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import to.bitkit.models.WidgetType
2222
import to.bitkit.utils.AppError
2323
import to.bitkit.utils.Logger
2424
import java.text.NumberFormat
25-
import java.util.Currency
2625
import java.util.Locale
2726
import javax.inject.Inject
2827
import 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+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package to.bitkit.data.widgets
2+
3+
import org.junit.Test
4+
import java.util.Locale
5+
import kotlin.test.assertEquals
6+
import kotlin.test.assertFalse
7+
8+
class PriceServiceTest {
9+
10+
@Test
11+
fun `formatPriceValue excludes currency symbol`() {
12+
val formatted = formatPriceValue(price = 81_444.12, locale = Locale.US)
13+
14+
assertEquals("81,444", formatted)
15+
assertFalse(formatted.contains('$'))
16+
}
17+
18+
@Test
19+
fun `formatPriceValue uses provided locale grouping`() {
20+
val formatted = formatPriceValue(price = 81_444.12, locale = Locale.GERMANY)
21+
22+
assertEquals("81.444", formatted)
23+
}
24+
25+
@Test
26+
fun `formatPriceValue keeps fractional precision by price range`() {
27+
assertEquals("81,444", formatPriceValue(price = 81_444.12, locale = Locale.US))
28+
assertEquals("814.12", formatPriceValue(price = 814.12, locale = Locale.US))
29+
assertEquals("0.123457", formatPriceValue(price = 0.1234567, locale = Locale.US))
30+
}
31+
}

0 commit comments

Comments
 (0)