Skip to content

Commit 6dd3156

Browse files
authored
feat: added bip177 balance format
1 parent 7c2c2f9 commit 6dd3156

File tree

4 files changed

+59
-1
lines changed

4 files changed

+59
-1
lines changed

BDKSwiftExampleWallet/Extensions/Int+Extensions.swift

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,16 @@ extension UInt64 {
3434
}
3535

3636
extension UInt64 {
37+
private var numberFormatter: NumberFormatter {
38+
let numberFormatter = NumberFormatter()
39+
numberFormatter.numberStyle = .decimal
40+
numberFormatter.usesGroupingSeparator = true
41+
numberFormatter.groupingSeparator = ","
42+
numberFormatter.generatesDecimalNumbers = false
43+
44+
return numberFormatter
45+
}
46+
3747
func formattedSatoshis() -> String {
3848
if self == 0 {
3949
return "0.00 000 000"
@@ -58,6 +68,17 @@ extension UInt64 {
5868
return formattedBalance
5969
}
6070
}
71+
72+
func formattedBip177() -> String {
73+
if self != .zero && self >= 1_000_000 && self % 1_000_000 == .zero {
74+
return "\(self / 1_000_000)M"
75+
76+
} else if self != .zero && self % 1_000 == 0 {
77+
return "\(self / 1_000)K"
78+
}
79+
80+
return numberFormatter.string(from: NSNumber(value: self)) ?? "0"
81+
}
6182
}
6283

6384
extension Int {

BDKSwiftExampleWallet/Model/BalanceDisplayFormat.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,12 @@ enum BalanceDisplayFormat: String, CaseIterable, Codable {
1313
case sats = "sats"
1414
case bip21q = "bip21q"
1515
case fiat = "usd"
16+
case bip177 = "bip177"
1617

1718
var displayText: String {
1819
switch self {
1920
case .sats, .bitcoinSats: return "sats"
20-
case .bitcoin: return ""
21+
case .bitcoin, .bip177: return ""
2122
case .bip21q: return ""
2223
case .fiat: return "USD"
2324
}

BDKSwiftExampleWallet/View/Home/BalanceView.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ struct BalanceView: View {
4848
return balance.formatted(.number)
4949
case .fiat:
5050
return satsPrice.formatted(.number.precision(.fractionLength(2)))
51+
case .bip177:
52+
return balance.formattedBip177()
5153
}
5254
}
5355

BDKSwiftExampleWalletTests/Extensions/BDKSwiftExampleWalletInt+Extensions.swift

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,38 @@ final class BDKSwiftExampleWalletInt_Extensions: XCTestCase {
3131
XCTAssertEqual(tenBTC, "10.00 000 001")
3232
}
3333

34+
func testBip177() {
35+
let oneHundred = UInt64(100).formattedBip177()
36+
XCTAssertEqual(oneHundred, "100")
37+
38+
let oneThousand = UInt64(1000).formattedBip177()
39+
XCTAssertEqual(oneThousand, "1K")
40+
41+
let oneThousandOne = UInt64(1001).formattedBip177()
42+
XCTAssertEqual(oneThousandOne, "1,001")
43+
44+
let tenThousand = UInt64(10000).formattedBip177()
45+
XCTAssertEqual(tenThousand, "10K")
46+
47+
let tenThousandOne = UInt64(10001).formattedBip177()
48+
XCTAssertEqual(tenThousandOne, "10,001")
49+
50+
let oneHundredThousand = UInt64(100000).formattedBip177()
51+
XCTAssertEqual(oneHundredThousand, "100K")
52+
53+
let oneHundredThousandOne = UInt64(100001).formattedBip177()
54+
XCTAssertEqual(oneHundredThousandOne, "100,001")
55+
56+
let oneMillion = UInt64(1000000).formattedBip177()
57+
XCTAssertEqual(oneMillion, "1M")
58+
59+
let oneMillionOne = UInt64(1000001).formattedBip177()
60+
XCTAssertEqual(oneMillionOne, "1,000,001")
61+
62+
let treeMillions = UInt64(325_000_000).formattedBip177()
63+
XCTAssertEqual(treeMillions, "325M")
64+
65+
let treeMillionsOne = UInt64(325_000_001).formattedBip177()
66+
XCTAssertEqual(treeMillionsOne, "325,000,001")
67+
}
3468
}

0 commit comments

Comments
 (0)