@@ -11,8 +11,7 @@ import java.util.Locale
1111const val BITCOIN_SYMBOL = " ₿"
1212const val SATS_IN_BTC = 100_000_000
1313const val BTC_SCALE = 8
14- const val BTC_PLACEHOLDER = " 0.00000000"
15- const val SATS_PLACEHOLDER = " 0"
14+ const val GROUPING_SEPARATOR = ' '
1615
1716@Serializable
1817data class FxRateResponse (
@@ -42,24 +41,22 @@ data class FxRate(
4241enum class PrimaryDisplay {
4342 BITCOIN , FIAT ;
4443
45- operator fun not (): PrimaryDisplay {
46- return when (this ) {
47- BITCOIN -> FIAT
48- FIAT -> BITCOIN
49- }
44+ operator fun not () = when (this ) {
45+ BITCOIN -> FIAT
46+ FIAT -> BITCOIN
5047 }
5148}
5249
5350/* * aka. Denomination */
5451enum class BitcoinDisplayUnit {
5552 MODERN , CLASSIC ;
5653
57- operator fun not (): BitcoinDisplayUnit {
58- return when (this ) {
59- MODERN -> CLASSIC
60- CLASSIC -> MODERN
61- }
54+ operator fun not () = when (this ) {
55+ MODERN -> CLASSIC
56+ CLASSIC -> MODERN
6257 }
58+
59+ fun isModern () = this == MODERN
6360}
6461
6562data class ConvertedAmount (
@@ -69,28 +66,17 @@ data class ConvertedAmount(
6966 val currency : String ,
7067 val flag : String ,
7168 val sats : Long ,
69+ val locale : Locale = Locale .getDefault(),
7270) {
73- val btcValue: BigDecimal = sats.asBtc()
74-
7571 data class BitcoinDisplayComponents (
7672 val symbol : String ,
7773 val value : String ,
7874 )
7975
8076 fun bitcoinDisplay (unit : BitcoinDisplayUnit ): BitcoinDisplayComponents {
81- val spaceSeparator = ' '
8277 val formattedValue = when (unit) {
83- BitcoinDisplayUnit .MODERN -> {
84- sats.formatToModernDisplay()
85- }
86-
87- BitcoinDisplayUnit .CLASSIC -> {
88- val formatSymbols = DecimalFormatSymbols (Locale .getDefault()).apply {
89- groupingSeparator = spaceSeparator
90- }
91- val formatter = DecimalFormat (" #,###.########" , formatSymbols)
92- formatter.format(btcValue)
93- }
78+ BitcoinDisplayUnit .MODERN -> sats.formatToModernDisplay(locale)
79+ BitcoinDisplayUnit .CLASSIC -> sats.formatToClassicDisplay(locale)
9480 }
9581 return BitcoinDisplayComponents (
9682 symbol = BITCOIN_SYMBOL ,
@@ -99,18 +85,25 @@ data class ConvertedAmount(
9985 }
10086}
10187
102- fun Long.formatToModernDisplay (): String {
88+ fun Long.formatToModernDisplay (locale : Locale = Locale .getDefault() ): String {
10389 val sats = this
104- val formatSymbols = DecimalFormatSymbols (Locale .getDefault() ).apply {
105- groupingSeparator = ' '
90+ val formatSymbols = DecimalFormatSymbols (locale ).apply {
91+ groupingSeparator = GROUPING_SEPARATOR
10692 }
10793 val formatter = DecimalFormat (" #,###" , formatSymbols).apply {
10894 isGroupingUsed = true
10995 }
11096 return formatter.format(sats)
11197}
11298
113- fun ULong.formatToModernDisplay (): String = this .toLong().formatToModernDisplay()
99+ fun ULong.formatToModernDisplay (locale : Locale = Locale .getDefault()): String = toLong().formatToModernDisplay(locale)
100+
101+ fun Long.formatToClassicDisplay (locale : Locale = Locale .getDefault()): String {
102+ val sats = this
103+ val formatSymbols = DecimalFormatSymbols (locale)
104+ val formatter = DecimalFormat (" ###.########" , formatSymbols)
105+ return formatter.format(sats.asBtc())
106+ }
114107
115108/* * Represent this sat value in Bitcoin BigDecimal. */
116109fun Long.asBtc (): BigDecimal = BigDecimal (this ).divide(BigDecimal (SATS_IN_BTC ), BTC_SCALE , RoundingMode .HALF_UP )
0 commit comments