Skip to content

feat: added balance ₿-only format(bip177) #299

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jul 17, 2025
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
21 changes: 21 additions & 0 deletions BDKSwiftExampleWallet/Extensions/Int+Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,16 @@ extension UInt64 {
}

extension UInt64 {
private var numberFormatter: NumberFormatter {
let numberFormatter = NumberFormatter()
numberFormatter.numberStyle = .decimal
numberFormatter.usesGroupingSeparator = true
numberFormatter.groupingSeparator = ","
numberFormatter.generatesDecimalNumbers = false

return numberFormatter
}

func formattedSatoshis() -> String {
if self == 0 {
return "0.00 000 000"
Expand All @@ -58,6 +68,17 @@ extension UInt64 {
return formattedBalance
}
}

func formattedBip177() -> String {
if self != .zero && self >= 1_000_000 && self % 1_000_000 == .zero {
return "\(self / 1_000_000)M"

} else if self != .zero && self % 1_000 == 0 {
return "\(self / 1_000)K"
}

return numberFormatter.string(from: NSNumber(value: self)) ?? "0"
}
}

extension Int {
Expand Down
3 changes: 2 additions & 1 deletion BDKSwiftExampleWallet/Model/BalanceDisplayFormat.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@ enum BalanceDisplayFormat: String, CaseIterable, Codable {
case sats = "sats"
case bip21q = "bip21q"
case fiat = "usd"
case bip177 = "bip177"

var displayText: String {
switch self {
case .sats, .bitcoinSats: return "sats"
case .bitcoin: return ""
case .bitcoin, .bip177: return ""
case .bip21q: return "₿"
case .fiat: return "USD"
}
Expand Down
2 changes: 2 additions & 0 deletions BDKSwiftExampleWallet/View/Home/BalanceView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ struct BalanceView: View {
return balance.formatted(.number)
case .fiat:
return satsPrice.formatted(.number.precision(.fractionLength(2)))
case .bip177:
return balance.formattedBip177()
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,38 @@ final class BDKSwiftExampleWalletInt_Extensions: XCTestCase {
XCTAssertEqual(tenBTC, "10.00 000 001")
}

func testBip177() {
let oneHundred = UInt64(100).formattedBip177()
XCTAssertEqual(oneHundred, "100")

let oneThousand = UInt64(1000).formattedBip177()
XCTAssertEqual(oneThousand, "1K")

let oneThousandOne = UInt64(1001).formattedBip177()
XCTAssertEqual(oneThousandOne, "1,001")

let tenThousand = UInt64(10000).formattedBip177()
XCTAssertEqual(tenThousand, "10K")

let tenThousandOne = UInt64(10001).formattedBip177()
XCTAssertEqual(tenThousandOne, "10,001")

let oneHundredThousand = UInt64(100000).formattedBip177()
XCTAssertEqual(oneHundredThousand, "100K")

let oneHundredThousandOne = UInt64(100001).formattedBip177()
XCTAssertEqual(oneHundredThousandOne, "100,001")

let oneMillion = UInt64(1000000).formattedBip177()
XCTAssertEqual(oneMillion, "1M")

let oneMillionOne = UInt64(1000001).formattedBip177()
XCTAssertEqual(oneMillionOne, "1,000,001")

let treeMillions = UInt64(325_000_000).formattedBip177()
XCTAssertEqual(treeMillions, "325M")

let treeMillionsOne = UInt64(325_000_001).formattedBip177()
XCTAssertEqual(treeMillionsOne, "325,000,001")
}
}