Repository Public
1// SP Wallet iOS2// File: Components/Money.swift3// Description: Formatting of what is displayed: fiat estimates, percentages, exact crypto amounts (grouped for reading, never4// rounded on an approval), relative ages. Formatting never changes a value that is sent to the server.5//6// Author: Simon-Pierre Boucher7// Contact: contact@spboucher.ai8import Foundation9import SPWalletCore1011enum Money {12 static func fiat(_ v: Double, _ currency: FiatCurrency, compact: Bool = false) -> String {13 let small = abs(v) > 0 && abs(v) < 114 var style = FloatingPointFormatStyle<Double>.Currency(code: currency.rawValue).precision(.fractionLength(small ? 2...4 : 2...2))15 if compact, abs(v) >= 100_000 { style = style.notation(.compactName).precision(.fractionLength(0...2)) }16 return v.formatted(style)17 }1819 static func price(_ v: Double, _ currency: FiatCurrency) -> String {20 let digits = v >= 1000 ? 2...2 : v >= 1 ? 2...4 : 2...821 return v.formatted(.currency(code: currency.rawValue).precision(.fractionLength(digits)))22 }2324 /** Signed percentage with its arrow, e.g. "▲ 2,31 %". The sign is in the text, not only in the colour. */25 static func change(_ pct: Double?) -> String? {26 guard let pct else { return nil }27 let s = abs(pct).formatted(.number.precision(.fractionLength(2)))28 return (pct > 0 ? "▲ " : pct < 0 ? "▼ " : "") + s + " %"29 }3031 /**32 The server's exact decimal text, with digit grouping for reading and the locale's decimal separator. No rounding:33 every digit the server gave is kept. Used wherever an amount will be approved.34 */35 static func exact(_ display: String, locale: Locale = .current) -> String {36 let parts = display.split(separator: ".", maxSplits: 1, omittingEmptySubsequences: false)37 guard let intPart = parts.first, intPart.allSatisfy(\.isNumber) else { return display }38 let group = locale.groupingSeparator ?? " ", dec = locale.decimalSeparator ?? "."39 var out = "", n = 040 for ch in intPart.reversed() { if n > 0, n % 3 == 0 { out.append(contentsOf: group.reversed()) }; out.append(ch); n += 1 }41 let i = String(out.reversed())42 return parts.count == 2 && !parts[1].isEmpty ? i + dec + parts[1] : i43 }4445 /** Shortened amount for lists (at most `maxFraction` decimals, truncated — never rounded up). Lists only, never an approval. */46 static func brief(_ display: String, maxFraction: Int = 6, locale: Locale = .current) -> String {47 let parts = display.split(separator: ".", maxSplits: 1, omittingEmptySubsequences: false)48 guard parts.count == 2, parts[1].count > maxFraction else { return exact(display, locale: locale) }49 var frac = String(parts[1].prefix(maxFraction))50 while frac.last == "0" { frac.removeLast() }51 let shortened = frac.isEmpty ? String(parts[0]) : parts[0] + "." + frac52 return exact(shortened, locale: locale) + "…"53 }5455 static func age(_ date: Date, now: Date = Date()) -> String {56 let s = now.timeIntervalSince(date)57 if s < 60 { return String(localized: "à l’instant") }58 return date.formatted(.relative(presentation: .numeric, unitsStyle: .abbreviated)) // "il y a 5 min" (the narrow style prints "-5 min")59 }6061 /** Middle-truncated identifier for LISTS (never on a review screen). */62 static func shortAddress(_ a: String) -> String { a.count <= 16 ? a : a.prefix(8) + "…" + a.suffix(6) }63}64