File tree Expand file tree Collapse file tree 2 files changed +47
-3
lines changed
main/java/to/bitkit/models
test/java/to/bitkit/models Expand file tree Collapse file tree 2 files changed +47
-3
lines changed Original file line number Diff line number Diff line change @@ -106,12 +106,16 @@ fun Long.formatToModernDisplay(locale: Locale = Locale.getDefault()): String {
106106fun ULong.formatToModernDisplay (locale : Locale = Locale .getDefault()): String = toLong().formatToModernDisplay(locale)
107107
108108fun 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
117121fun BigDecimal.formatCurrency (decimalPlaces : Int = FIAT_DECIMALS , locale : Locale = Locale .getDefault()): String? {
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments