Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions app/src/main/java/to/bitkit/models/Currency.kt
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,16 @@ fun Long.formatToModernDisplay(locale: Locale = Locale.getDefault()): String {
fun ULong.formatToModernDisplay(locale: Locale = Locale.getDefault()): String = toLong().formatToModernDisplay(locale)

fun Long.formatToClassicDisplay(locale: Locale = Locale.getDefault()): String {
val sats = this
val symbols = DecimalFormatSymbols(locale).apply {
decimalSeparator = DECIMAL_SEPARATOR
}
val formatter = DecimalFormat("###.########", symbols)
return formatter.format(sats.asBtc())
val pattern = "0.${"0".repeat(CLASSIC_DECIMALS)}"
val formatter = DecimalFormat(pattern, symbols).apply {
minimumFractionDigits = CLASSIC_DECIMALS
maximumFractionDigits = CLASSIC_DECIMALS
isGroupingUsed = false
}
return formatter.format(asBtc())
}

fun BigDecimal.formatCurrency(decimalPlaces: Int = FIAT_DECIMALS, locale: Locale = Locale.getDefault()): String? {
Expand Down
40 changes: 40 additions & 0 deletions app/src/test/java/to/bitkit/models/CurrencyTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package to.bitkit.models

import org.junit.Assert.assertEquals
import org.junit.Test
import java.util.Locale

class CurrencyTest {

@Test
fun `formatToModernDisplay uses space grouping`() {
val sats = 123_456_789L

val formatted = sats.formatToModernDisplay(Locale.US)

assertEquals("123 456 789", formatted)
}

@Test
fun `formatToModernDisplay handles zero`() {
val formatted = 0L.formatToModernDisplay(Locale.US)

assertEquals("0", formatted)
}

@Test
fun `formatToClassicDisplay always shows eight decimals`() {
val formatted = 0L.formatToClassicDisplay(Locale.US)

assertEquals("0.00000000", formatted)
}

@Test
fun `formatToClassicDisplay converts sats to btc`() {
val sats = 12_345L // 0.00012345 BTC

val formatted = sats.formatToClassicDisplay(Locale.US)

assertEquals("0.00012345", formatted)
}
}
Loading