Improve formatting of numbers by adding grouping separators and limiting the number of fractional digits

This commit is contained in:
2025-03-30 09:47:00 -04:00
parent 5cd4d9189b
commit 82dbf16466
4 changed files with 176 additions and 69 deletions

View File

@@ -33,17 +33,6 @@ public struct ContentView: View {
) )
} }
public func selectedCurrencyBinding(_ currency: Locale.Currency) -> Binding<String> {
Binding(
get: {
satsViewModel.currencyValueStrings[currency, default: ""]
},
set: { priceString in
satsViewModel.currencyValueStrings[currency] = priceString
}
)
}
public var body: some View { public var body: some View {
NavigationStack { NavigationStack {
Form { Form {
@@ -96,7 +85,7 @@ public struct ContentView: View {
} }
} footer: { } footer: {
if satsViewModel.exceedsMaximum { if satsViewModel.exceedsMaximum {
Text("21000000 BTC is the maximum.") Text("\(SatsViewModel.MAXIMUM_BTC.formatBTCString()) BTC is the maximum.")
} }
} }

View File

@@ -14,10 +14,10 @@
} }
} }
}, },
"1 BTC to %@" : { "%@ BTC is the maximum." : {
}, },
"21000000 BTC is the maximum." : { "1 BTC to %@" : {
}, },
"BTC" : { "BTC" : {

View File

@@ -13,6 +13,10 @@ import Foundation
import SwiftUI import SwiftUI
class SatsViewModel: ObservableObject { class SatsViewModel: ObservableObject {
static let MAXIMUM_BTC = Decimal(21000000)
private static let SATS_IN_BTC = Decimal(100000000)
let model: SatsPriceModel let model: SatsPriceModel
@Published var lastUpdated: Date? @Published var lastUpdated: Date?
@@ -25,7 +29,8 @@ class SatsViewModel: ObservableObject {
@Published var selectedCurrencies = Set<Locale.Currency>() @Published var selectedCurrencies = Set<Locale.Currency>()
@Published var currencyValueStrings: [Locale.Currency: String] = [:] @Published var currencyValueStrings: [Locale.Currency: String] = [:]
var currencyPrices: [Locale.Currency: Decimal] = [:] @Published var currencyPrices: [Locale.Currency: Decimal] = [:]
@Published var currencyPriceStrings: [Locale.Currency: String] = [:]
let currentCurrency: Locale.Currency = Locale.current.currency ?? Locale.Currency("USD") let currentCurrency: Locale.Currency = Locale.current.currency ?? Locale.Currency("USD")
@@ -90,6 +95,7 @@ class SatsViewModel: ObservableObject {
let prices = try await priceFetcherDelegator.convertBTC(toCurrencies: Array(currencies)) let prices = try await priceFetcherDelegator.convertBTC(toCurrencies: Array(currencies))
currencyPrices = prices currencyPrices = prices
updateCurrencyPriceStrings()
updateCurrencyValueStrings() updateCurrencyValueStrings()
} catch { } catch {
clearCurrencyValueStrings() clearCurrencyValueStrings()
@@ -97,24 +103,49 @@ class SatsViewModel: ObservableObject {
lastUpdated = Date.now lastUpdated = Date.now
} }
func updateCurrencyPriceStrings() {
currencyPriceStrings = Dictionary(
uniqueKeysWithValues: currencyPrices.map { ($0.key, $0.value.formatString(currency: $0.key)) }
)
}
private func priceWithoutGroupingSeparator(_ priceString: String) -> String {
let numberFormatter = NumberFormatter()
numberFormatter.numberStyle = .decimal
let decimalSeparator = numberFormatter.decimalSeparator
return priceString.filter {
$0.isDigit || String($0) == decimalSeparator
}
}
var satsString: String { var satsString: String {
get { get {
satsStringInternal satsStringInternal
} }
set { set {
guard satsStringInternal != newValue else { let oldPriceWithoutGroupingSeparator = priceWithoutGroupingSeparator(satsStringInternal)
let newPriceWithoutGroupingSeparator = priceWithoutGroupingSeparator(newValue)
guard oldPriceWithoutGroupingSeparator != newPriceWithoutGroupingSeparator else {
return return
} }
satsStringInternal = newValue satsStringInternal = newPriceWithoutGroupingSeparator
if let sats { if let sats {
#if !SKIP #if !SKIP
let btc = sats / Decimal(100000000) // Formatting the internal string after modifying it only if the platform is Apple.
// Apple does not seem to call get after set until after focus is moved to a different component.
// Android, on the other hand, does call get immediately after set,
// which causes text entry issues if the user keeps on entering input.
satsStringInternal = sats.formatSatsString()
let btc = sats / SatsViewModel.SATS_IN_BTC
#else #else
let btc = sats.divide(Decimal(100000000), 20, java.math.RoundingMode.DOWN) let btc = sats.divide(SatsViewModel.SATS_IN_BTC, 20, java.math.RoundingMode.DOWN)
#endif #endif
btcStringInternal = btc.formatString() btcStringInternal = btc.formatBTCString()
updateCurrencyValueStrings() updateCurrencyValueStrings()
} else { } else {
@@ -129,15 +160,26 @@ class SatsViewModel: ObservableObject {
btcStringInternal btcStringInternal
} }
set { set {
guard btcStringInternal != newValue else { let oldPriceWithoutGroupingSeparator = priceWithoutGroupingSeparator(btcStringInternal)
let newPriceWithoutGroupingSeparator = priceWithoutGroupingSeparator(newValue)
guard oldPriceWithoutGroupingSeparator != newPriceWithoutGroupingSeparator else {
return return
} }
btcStringInternal = newValue btcStringInternal = newPriceWithoutGroupingSeparator
if let btc { if let btc {
let sats = btc * Decimal(100000000) #if !SKIP
satsStringInternal = sats.formatString() // Formatting the internal string after modifying it only if the platform is Apple.
// Apple does not seem to call get after set until after focus is moved to a different component.
// Android, on the other hand, does call get immediately after set,
// which causes text entry issues if the user keeps on entering input.
btcStringInternal = btc.formatBTCString()
#endif
let sats = btc * SatsViewModel.SATS_IN_BTC
satsStringInternal = sats.formatSatsString()
updateCurrencyValueStrings() updateCurrencyValueStrings()
} else { } else {
@@ -154,7 +196,7 @@ class SatsViewModel: ObservableObject {
for currency in currencies { for currency in currencies {
if let btcToCurrency = btcToCurrency(for: currency) { if let btcToCurrency = btcToCurrency(for: currency) {
currencyValueStrings[currency] = (btc * btcToCurrency).formatString() currencyValueStrings[currency] = (btc * btcToCurrency).formatString(currency: currency)
} else { } else {
currencyValueStrings[currency] = "" currencyValueStrings[currency] = ""
} }
@@ -176,11 +218,14 @@ class SatsViewModel: ObservableObject {
self.currencyValueStrings[currency, default: ""] self.currencyValueStrings[currency, default: ""]
}, },
set: { newValue in set: { newValue in
guard self.currencyValueStrings[currency] != newValue else { let oldPriceWithoutGroupingSeparator = self.priceWithoutGroupingSeparator(self.currencyValueStrings[currency] ?? "")
let newPriceWithoutGroupingSeparator = self.priceWithoutGroupingSeparator(newValue)
guard oldPriceWithoutGroupingSeparator != newPriceWithoutGroupingSeparator else {
return return
} }
self.currencyValueStrings[currency] = newValue self.currencyValueStrings[currency] = newPriceWithoutGroupingSeparator
if let currencyValue = self.currencyValue(for: currency) { if let currencyValue = self.currencyValue(for: currency) {
if let btcToCurrency = self.currencyPrices[currency] { if let btcToCurrency = self.currencyPrices[currency] {
@@ -189,12 +234,20 @@ class SatsViewModel: ObservableObject {
#else #else
let btc = currencyValue.divide(btcToCurrency, 20, java.math.RoundingMode.DOWN) let btc = currencyValue.divide(btcToCurrency, 20, java.math.RoundingMode.DOWN)
#endif #endif
self.btcStringInternal = btc.formatString() self.btcStringInternal = btc.formatBTCString()
let sats = btc * Decimal(100000000) let sats = btc * SatsViewModel.SATS_IN_BTC
self.satsStringInternal = sats.formatString() self.satsStringInternal = sats.formatSatsString()
#if !SKIP
// Formatting the internal string after modifying it only if the platform is Apple.
// Apple does not seem to call get after set until after focus is moved to a different component.
// Android, on the other hand, does call get immediately after set,
// which causes text entry issues if the user keeps on entering input.
self.updateCurrencyValueStrings(excludedCurrency: nil)
#else
self.updateCurrencyValueStrings(excludedCurrency: currency) self.updateCurrencyValueStrings(excludedCurrency: currency)
#endif
} else { } else {
self.satsStringInternal = "" self.satsStringInternal = ""
self.btcStringInternal = "" self.btcStringInternal = ""
@@ -215,7 +268,7 @@ class SatsViewModel: ObservableObject {
} }
#if !SKIP #if !SKIP
return Decimal(string: currencyValueString) return Decimal(string: priceWithoutGroupingSeparator(currencyValueString))
#else #else
do { do {
return Decimal(currencyValueString) return Decimal(currencyValueString)
@@ -232,23 +285,38 @@ class SatsViewModel: ObservableObject {
func btcToCurrencyString(for currency: Locale.Currency) -> Binding<String> { func btcToCurrencyString(for currency: Locale.Currency) -> Binding<String> {
Binding<String>( Binding<String>(
get: { get: {
self.currencyPrices[currency]?.formatString() ?? "" self.currencyPriceStrings[currency, default: ""]
}, },
set: { newValue in set: { newValue in
let oldPriceWithoutGroupingSeparator = self.priceWithoutGroupingSeparator(self.currencyPriceStrings[currency, default: ""])
let newPriceWithoutGroupingSeparator = self.priceWithoutGroupingSeparator(newValue)
guard oldPriceWithoutGroupingSeparator != newPriceWithoutGroupingSeparator else {
return
}
self.currencyPriceStrings[currency] = newPriceWithoutGroupingSeparator
#if !SKIP #if !SKIP
if let newPrice = Decimal(string: newValue), self.currencyPrices[currency] != newPrice { if let newPrice = Decimal(string: newPriceWithoutGroupingSeparator), self.currencyPrices[currency] != newPrice {
self.currencyPrices[currency] = Decimal(string: newValue) self.currencyPrices[currency] = newPrice
// Formatting the internal string after modifying it only if the platform is Apple.
// Apple does not seem to call get after set until after focus is moved to a different
// component. Android, on the other hand, does call get immediately after set,
// which causes text entry issues if the user keeps on entering input.
self.currencyPriceStrings[currency] = newPrice.formatString(currency: currency)
if let btc = self.btc { if let btc = self.btc {
self.currencyValueStrings[currency] = (btc * newPrice).formatString() self.currencyValueStrings[currency] = (btc * newPrice).formatString(currency: currency)
} else { } else {
self.currencyValueStrings[currency] = "" self.currencyValueStrings[currency] = ""
} }
} }
#else #else
do { do {
if let newPrice = Decimal(newValue), self.currencyPrices[currency] != newPrice { if let newPrice = Decimal(newPriceWithoutGroupingSeparator), self.currencyPrices[currency] != newPrice {
self.currencyPrices[currency] = Decimal(newValue) self.currencyPrices[currency] = newPrice
if let btc = self.btc { if let btc = self.btc {
self.currencyValueStrings[currency] = (btc * newPrice).formatString() self.currencyValueStrings[currency] = (btc * newPrice).formatString()
@@ -265,11 +333,12 @@ class SatsViewModel: ObservableObject {
} }
var sats: Decimal? { var sats: Decimal? {
let priceWithoutGroupingSeparator = priceWithoutGroupingSeparator(satsStringInternal)
#if !SKIP #if !SKIP
return Decimal(string: satsStringInternal) return Decimal(string: priceWithoutGroupingSeparator)
#else #else
do { do {
return Decimal(satsStringInternal) return Decimal(priceWithoutGroupingSeparator)
} catch { } catch {
return nil return nil
} }
@@ -277,11 +346,12 @@ class SatsViewModel: ObservableObject {
} }
var btc: Decimal? { var btc: Decimal? {
let priceWithoutGroupingSeparator = priceWithoutGroupingSeparator(btcStringInternal)
#if !SKIP #if !SKIP
return Decimal(string: btcStringInternal) return Decimal(string: priceWithoutGroupingSeparator)
#else #else
do { do {
return Decimal(btcStringInternal) return Decimal(priceWithoutGroupingSeparator)
} catch { } catch {
return nil return nil
} }
@@ -289,7 +359,7 @@ class SatsViewModel: ObservableObject {
} }
var exceedsMaximum: Bool { var exceedsMaximum: Bool {
if let btc, btc > Decimal(21000000) { if let btc, btc > SatsViewModel.MAXIMUM_BTC {
return true return true
} }
return false return false
@@ -304,4 +374,52 @@ extension Decimal {
return stripTrailingZeros().toPlainString() return stripTrailingZeros().toPlainString()
#endif #endif
} }
func formatString(minimumFractionDigits: Int, maximumFractionDigits: Int, usesGroupingSeparator: Bool) -> String {
let numberFormatter = NumberFormatter()
numberFormatter.numberStyle = .decimal
numberFormatter.minimumFractionDigits = minimumFractionDigits
numberFormatter.maximumFractionDigits = maximumFractionDigits
numberFormatter.usesGroupingSeparator = usesGroupingSeparator
#if !SKIP
return numberFormatter.string(from: NSDecimalNumber(decimal: self)) ?? String(describing: self)
#else
return numberFormatter.string(from: android.icu.math.BigDecimal(self as java.math.BigDecimal) as NSNumber) ?? stripTrailingZeros().toPlainString()
#endif
}
func formatSatsString() -> String {
formatString(minimumFractionDigits: 0, maximumFractionDigits: 0, usesGroupingSeparator: true)
}
func formatBTCString() -> String {
formatString(minimumFractionDigits: 0, maximumFractionDigits: 8, usesGroupingSeparator: true)
}
func formatString(currency: Locale.Currency) -> String {
#if !SKIP
let currencyFormatter = NumberFormatter()
currencyFormatter.numberStyle = .currency
currencyFormatter.currencyCode = currency.identifier
return formatString(
minimumFractionDigits: currencyFormatter.minimumFractionDigits,
maximumFractionDigits: currencyFormatter.maximumFractionDigits,
usesGroupingSeparator: currencyFormatter.usesGroupingSeparator
)
#else
let javaCurrency = java.util.Currency.getInstance(currency.identifier)
return formatString(
minimumFractionDigits: javaCurrency.getDefaultFractionDigits(),
maximumFractionDigits: javaCurrency.getDefaultFractionDigits(),
usesGroupingSeparator: true
)
#endif
}
}
private extension Character {
var isDigit: Bool {
self >= "0" && self <= "9"
}
} }

View File

@@ -16,8 +16,9 @@ final class SatsViewModelTests: XCTestCase {
let currency = Locale.Currency("USD") let currency = Locale.Currency("USD")
func testSatsViewModel() { func testSatsViewModel() throws {
let satsViewModel = SatsViewModel() let satsPriceModel = try XCTUnwrap(SatsPriceModel(url: nil))
let satsViewModel = SatsViewModel(model: satsPriceModel)
satsViewModel.btcToCurrencyString(for: currency).wrappedValue = "54321" satsViewModel.btcToCurrencyString(for: currency).wrappedValue = "54321"
// Test BTC updates. // Test BTC updates.
@@ -32,8 +33,8 @@ final class SatsViewModelTests: XCTestCase {
XCTAssertEqual(satsViewModel.currencyValue(for: currency), Decimal("54321")) XCTAssertEqual(satsViewModel.currencyValue(for: currency), Decimal("54321"))
#endif #endif
XCTAssertEqual(satsViewModel.btcString, "1") XCTAssertEqual(satsViewModel.btcString, "1")
XCTAssertEqual(satsViewModel.satsString, "100000000") XCTAssertEqual(satsViewModel.satsString, "100,000,000")
XCTAssertEqual(satsViewModel.currencyValueString(for: currency).wrappedValue, "54321") XCTAssertEqual(satsViewModel.currencyValueString(for: currency).wrappedValue, "54,321.00")
// Test Sats updates. // Test Sats updates.
satsViewModel.satsString = "200000000" satsViewModel.satsString = "200000000"
@@ -47,8 +48,8 @@ final class SatsViewModelTests: XCTestCase {
XCTAssertEqual(satsViewModel.currencyValue(for: currency), Decimal("108642")) XCTAssertEqual(satsViewModel.currencyValue(for: currency), Decimal("108642"))
#endif #endif
XCTAssertEqual(satsViewModel.btcString, "2") XCTAssertEqual(satsViewModel.btcString, "2")
XCTAssertEqual(satsViewModel.satsString, "200000000") XCTAssertEqual(satsViewModel.satsString, "200,000,000")
XCTAssertEqual(satsViewModel.currencyValueString(for: currency).wrappedValue, "108642") XCTAssertEqual(satsViewModel.currencyValueString(for: currency).wrappedValue, "108,642.00")
// Test currency value updates. // Test currency value updates.
satsViewModel.currencyValueString(for: currency).wrappedValue = "162963" satsViewModel.currencyValueString(for: currency).wrappedValue = "162963"
@@ -62,44 +63,43 @@ final class SatsViewModelTests: XCTestCase {
XCTAssertEqual(satsViewModel.currencyValue(for: currency), Decimal("162963")) XCTAssertEqual(satsViewModel.currencyValue(for: currency), Decimal("162963"))
#endif #endif
XCTAssertEqual(satsViewModel.btcString, "3") XCTAssertEqual(satsViewModel.btcString, "3")
XCTAssertEqual(satsViewModel.satsString, "300000000") XCTAssertEqual(satsViewModel.satsString, "300,000,000")
XCTAssertEqual(satsViewModel.currencyValueString(for: currency).wrappedValue, "162963") XCTAssertEqual(satsViewModel.currencyValueString(for: currency).wrappedValue, "162,963.00")
// Test fractional amounts. // Test fractional amounts.
// Precision between platforms on this calculation is different so we have different assertions for each. // Precision between platforms on this calculation is different so we have different assertions for each.
satsViewModel.currencyValueString(for: currency).wrappedValue = "1" satsViewModel.currencyValueString(for: currency).wrappedValue = "1"
#if !SKIP #if !SKIP
XCTAssertEqual(satsViewModel.btc, Decimal(string: "0.00001840908672520756245282671526665562")) XCTAssertEqual(satsViewModel.btc, Decimal(string: "0.00001841"))
XCTAssertEqual(satsViewModel.btcString, "0.00001840908672520756245282671526665562") XCTAssertEqual(satsViewModel.btcString, "0.00001841")
XCTAssertEqual(satsViewModel.sats, Decimal(string: "1840.908672520756245282671526665562")) XCTAssertEqual(satsViewModel.sats, Decimal(string: "1841"))
XCTAssertEqual(satsViewModel.satsString, "1840.908672520756245282671526665562") XCTAssertEqual(satsViewModel.satsString, "1,841")
XCTAssertEqual(satsViewModel.currencyValue(for: currency), Decimal(string: "1")) XCTAssertEqual(satsViewModel.currencyValue(for: currency), Decimal(string: "1"))
#else #else
XCTAssertEqual(satsViewModel.btc, Decimal("0.00001840908672520756")) XCTAssertEqual(satsViewModel.btc, Decimal("0.00001841"))
XCTAssertEqual(satsViewModel.btcString, "0.00001840908672520756") XCTAssertEqual(satsViewModel.btcString, "0.00001841")
XCTAssertEqual(satsViewModel.sats, Decimal("1840.908672520756")) XCTAssertEqual(satsViewModel.sats, Decimal("1841"))
XCTAssertEqual(satsViewModel.satsString, "1840.908672520756") XCTAssertEqual(satsViewModel.satsString, "1,841")
XCTAssertEqual(satsViewModel.currencyValue(for: currency), Decimal("1")) XCTAssertEqual(satsViewModel.currencyValue(for: currency), Decimal("1"))
#endif #endif
XCTAssertEqual(satsViewModel.currencyValueString(for: currency).wrappedValue, "1") XCTAssertEqual(satsViewModel.currencyValueString(for: currency).wrappedValue, "1.00")
// Test large amounts that exceed the cap of 21M BTC. // Test large amounts that exceed the cap of 21M BTC.
// Precision between platforms on this calculation is different so we have different assertions for each.
satsViewModel.currencyValueString(for: currency).wrappedValue = "11407419999999" satsViewModel.currencyValueString(for: currency).wrappedValue = "11407419999999"
#if !SKIP #if !SKIP
XCTAssertEqual(satsViewModel.btc, Decimal(string: "210000184.09084884298889932070469983984")) XCTAssertEqual(satsViewModel.btc, Decimal(string: "210000184.09084884"))
XCTAssertEqual(satsViewModel.btcString, "210000184.09084884298889932070469983984") XCTAssertEqual(satsViewModel.btcString, "210,000,184.09084884")
XCTAssertEqual(satsViewModel.sats, Decimal(string: "21000018409084884.298889932070469983984")) XCTAssertEqual(satsViewModel.sats, Decimal(string: "21000018409084884"))
XCTAssertEqual(satsViewModel.satsString, "21000018409084884.298889932070469983984") XCTAssertEqual(satsViewModel.satsString, "21,000,018,409,084,884")
XCTAssertEqual(satsViewModel.currencyValue(for: currency), Decimal(string: "11407419999999")) XCTAssertEqual(satsViewModel.currencyValue(for: currency), Decimal(string: "11407419999999"))
#else #else
XCTAssertEqual(satsViewModel.btc, Decimal("210000184.0908488429888993207")) XCTAssertEqual(satsViewModel.btc, Decimal("210000184.09084884"))
XCTAssertEqual(satsViewModel.btcString, "210000184.0908488429888993207") XCTAssertEqual(satsViewModel.btcString, "210000184.09084884")
XCTAssertEqual(satsViewModel.sats, Decimal("21000018409084884.29888993207")) XCTAssertEqual(satsViewModel.sats, Decimal("21000018409084884"))
XCTAssertEqual(satsViewModel.satsString, "21000018409084884.29888993207") XCTAssertEqual(satsViewModel.satsString, "21000018409084884")
XCTAssertEqual(satsViewModel.currencyValue, Decimal("11407419999999")) XCTAssertEqual(satsViewModel.currencyValue, Decimal("11407419999999"))
#endif #endif
XCTAssertEqual(satsViewModel.currencyValueString(for: currency).wrappedValue, "11407419999999") XCTAssertEqual(satsViewModel.currencyValueString(for: currency).wrappedValue, "11,407,419,999,999.00")
} }
} }