Skip to content

Commit 5ebe394

Browse files
authored
Merge pull request #467 from synonymdev/fix/zero-denomination
fix: show 0 balance with decimals in classic denomination
2 parents 24be4bc + 3e7b5f6 commit 5ebe394

File tree

2 files changed

+47
-3
lines changed

2 files changed

+47
-3
lines changed

app/src/main/java/to/bitkit/models/Currency.kt

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,12 +106,16 @@ fun Long.formatToModernDisplay(locale: Locale = Locale.getDefault()): String {
106106
fun ULong.formatToModernDisplay(locale: Locale = Locale.getDefault()): String = toLong().formatToModernDisplay(locale)
107107

108108
fun Long.formatToClassicDisplay(locale: Locale = Locale.getDefault()): String {
109-
val sats = this
110109
val symbols = DecimalFormatSymbols(locale).apply {
111110
decimalSeparator = DECIMAL_SEPARATOR
112111
}
113-
val formatter = DecimalFormat("###.########", symbols)
114-
return formatter.format(sats.asBtc())
112+
val pattern = "0.${"0".repeat(CLASSIC_DECIMALS)}"
113+
val formatter = DecimalFormat(pattern, symbols).apply {
114+
minimumFractionDigits = CLASSIC_DECIMALS
115+
maximumFractionDigits = CLASSIC_DECIMALS
116+
isGroupingUsed = false
117+
}
118+
return formatter.format(asBtc())
115119
}
116120

117121
fun BigDecimal.formatCurrency(decimalPlaces: Int = FIAT_DECIMALS, locale: Locale = Locale.getDefault()): String? {
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package to.bitkit.models
2+
3+
import org.junit.Assert.assertEquals
4+
import org.junit.Test
5+
import java.util.Locale
6+
7+
class CurrencyTest {
8+
9+
@Test
10+
fun `formatToModernDisplay uses space grouping`() {
11+
val sats = 123_456_789L
12+
13+
val formatted = sats.formatToModernDisplay(Locale.US)
14+
15+
assertEquals("123 456 789", formatted)
16+
}
17+
18+
@Test
19+
fun `formatToModernDisplay handles zero`() {
20+
val formatted = 0L.formatToModernDisplay(Locale.US)
21+
22+
assertEquals("0", formatted)
23+
}
24+
25+
@Test
26+
fun `formatToClassicDisplay always shows eight decimals`() {
27+
val formatted = 0L.formatToClassicDisplay(Locale.US)
28+
29+
assertEquals("0.00000000", formatted)
30+
}
31+
32+
@Test
33+
fun `formatToClassicDisplay converts sats to btc`() {
34+
val sats = 12_345L // 0.00012345 BTC
35+
36+
val formatted = sats.formatToClassicDisplay(Locale.US)
37+
38+
assertEquals("0.00012345", formatted)
39+
}
40+
}

0 commit comments

Comments
 (0)