|
| 1 | +// |
| 2 | +// Formatter.swift |
| 3 | +// Units |
| 4 | +// (aka Fountation.FormatStyle) |
| 5 | +// |
| 6 | +// Created by Jason Jobe on 10/24/25. |
| 7 | +// |
| 8 | + |
| 9 | +public extension Measurement { |
| 10 | + struct Formatter<Output> { |
| 11 | + let format: (Measurement) -> Output |
| 12 | + } |
| 13 | + |
| 14 | + func formatted<Output>(_ formatter: Formatter<Output>) -> Output { |
| 15 | + formatter.format(self) |
| 16 | + } |
| 17 | + |
| 18 | + func formatted(_ formatter: Formatter<String> = .measurement()) -> String { |
| 19 | + formatter.format(self) |
| 20 | + } |
| 21 | + |
| 22 | + func formatted( |
| 23 | + minimumFractionDigits: Int = 0, |
| 24 | + maximumFractionDigits: Int = 4 |
| 25 | + ) -> String { |
| 26 | + Formatter |
| 27 | + .measurement( |
| 28 | + minimumFractionDigits: minimumFractionDigits, |
| 29 | + maximumFractionDigits: maximumFractionDigits) |
| 30 | + .format(self) |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +extension Measurement.Formatter where Output == String { |
| 35 | + public static func measurement( |
| 36 | + minimumFractionDigits: Int = 0, |
| 37 | + maximumFractionDigits: Int = 4 |
| 38 | + ) -> Self { |
| 39 | + self.init { value in |
| 40 | + value.value |
| 41 | + .formatted( |
| 42 | + minimumFractionDigits: minimumFractionDigits, |
| 43 | + maximumFractionDigits: maximumFractionDigits) |
| 44 | + + " \(value.unit.symbol)" |
| 45 | + } |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | + |
| 50 | +// MARK: Percent Formatter |
| 51 | + |
| 52 | +public extension Percent { |
| 53 | + struct Formatter<Output> { |
| 54 | + let format: (Percent) -> Output |
| 55 | + } |
| 56 | + |
| 57 | + func formatted<Output>(_ formatter: Formatter<Output>) -> Output { |
| 58 | + formatter.format(self) |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +public extension Percent { |
| 63 | + func formatted(fractionDigits: Int = 2) -> String { |
| 64 | + Formatter(fractionDigits: fractionDigits).format(self) |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +public extension Percent.Formatter where Output == String { |
| 69 | + |
| 70 | + init (fractionDigits: Int) { |
| 71 | + self.init { value in |
| 72 | + (value.magnitude * 100) |
| 73 | + .formatted( |
| 74 | + minimumFractionDigits: 0, |
| 75 | + maximumFractionDigits: fractionDigits) |
| 76 | + + value.unit.symbol |
| 77 | + } |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +public extension BinaryFloatingPoint { |
| 82 | + func formatted(minimumFractionDigits: Int = 0, maximumFractionDigits: Int = 4) -> String { |
| 83 | + let minDigits = max(0, minimumFractionDigits) |
| 84 | + let maxDigits = max(minDigits, maximumFractionDigits) |
| 85 | + let s = String(format: "%.\(maxDigits)f", Double(self)) |
| 86 | + if maxDigits > minDigits, s.contains(".") { |
| 87 | + var trimmed = s |
| 88 | + while trimmed.last == "0" { trimmed.removeLast() } |
| 89 | + if trimmed.last == "." { trimmed.removeLast() } |
| 90 | + if let dotIndex = trimmed.firstIndex(of: ".") { |
| 91 | + let fractionalCount = trimmed.distance(from: trimmed.index(after: dotIndex), to: trimmed.endIndex) |
| 92 | + if fractionalCount < minDigits { |
| 93 | + return String(format: "%.\(minDigits)f", Double(self)) |
| 94 | + } |
| 95 | + } |
| 96 | + return trimmed |
| 97 | + } |
| 98 | + return s |
| 99 | + } |
| 100 | +} |
0 commit comments