diff --git a/BDKSwiftExampleWallet/Extensions/Int+Extensions.swift b/BDKSwiftExampleWallet/Extensions/Int+Extensions.swift index 1802ca84..319a7e14 100644 --- a/BDKSwiftExampleWallet/Extensions/Int+Extensions.swift +++ b/BDKSwiftExampleWallet/Extensions/Int+Extensions.swift @@ -38,22 +38,28 @@ extension UInt64 { if self == 0 { return "0.00 000 000" } else { - let balanceString = String(format: "%010d", self) - - let zero = balanceString.prefix(2) - let first = balanceString.dropFirst(2).prefix(2) - let second = balanceString.dropFirst(4).prefix(3) - let third = balanceString.dropFirst(7).prefix(3) - - var formattedZero = zero - - if zero == "00" { - formattedZero = zero.dropFirst() - } else if zero.hasPrefix("0") { - formattedZero = zero.suffix(1) - } - - let formattedBalance = "\(formattedZero).\(first) \(second) \(third)" + // Convert satoshis to BTC (1 BTC = 100,000,000 sats) + let btcValue = Double(self) / 100_000_000.0 + + // Format BTC value to exactly 8 decimal places + let btcString = String(format: "%.8f", btcValue) + + // Split the string at the decimal point + let parts = btcString.split(separator: ".") + guard parts.count == 2 else { return btcString } + + let wholePart = String(parts[0]) + let decimalPart = String(parts[1]) + + // Ensure decimal part is exactly 8 digits + let paddedDecimal = decimalPart.padding(toLength: 8, withPad: "0", startingAt: 0) + + // Format as XX.XX XXX XXX + let first = paddedDecimal.prefix(2) + let second = paddedDecimal.dropFirst(2).prefix(3) + let third = paddedDecimal.dropFirst(5).prefix(3) + + let formattedBalance = "\(wholePart).\(first) \(second) \(third)" return formattedBalance }