Repository Public
1import Foundation2import SheetCore3import Statistics45/// How money amounts are rounded to the currency's minor unit.6public struct MoneyRounding: Sendable, Hashable, Codable {7 public enum Mode: String, Sendable, Codable, CaseIterable {8 /// Round half away from zero (what banks and Excel's ROUND do): 0.125 → 0.13, −0.125 → −0.13.9 case halfUp10 /// Round half to even ("banker's rounding"): 0.125 → 0.12, 0.135 → 0.14.11 case halfEven12 /// Truncate toward zero.13 case down14 /// Round away from zero.15 case up16 }17 public var mode: Mode18 /// Number of decimal places kept (2 = cents).19 public var scale: Int2021 public init(mode: Mode = .halfUp, scale: Int = 2) { self.mode = mode; self.scale = scale }2223 /// Cents, half away from zero.24 public static let cents = MoneyRounding()2526 public func round(_ value: Decimal) -> Decimal {27 var input = value, output = Decimal()28 let negative = value < 029 let nsMode: NSDecimalNumber.RoundingMode30 switch mode {31 case .halfUp: nsMode = .plain32 case .halfEven: nsMode = .bankers33 case .down: nsMode = negative ? .up : .down34 case .up: nsMode = negative ? .down : .up35 }36 NSDecimalRound(&output, &input, scale, nsMode)37 return output38 }3940 /// Rounds a binary floating-point amount: it is first converted to its shortest decimal41 /// representation (17 significant digits at most), then rounded as a decimal.42 public func round(_ value: Double) -> Decimal { round(Money.decimal(value)) }43}4445/// Conversions between `Double` (rates, solvers) and `Decimal` (money).46public enum Money {47 /// Exact decimal value of the shortest string that round-trips the double (e.g. 0.1 → 0.1, not 0.1000000000000000055…).48 public static func decimal(_ d: Double) -> Decimal {49 guard d.isFinite else { return Decimal.nan }50 return Decimal(string: "\(d)", locale: Locale(identifier: "en_US_POSIX")) ?? Decimal(d)51 }5253 public static func double(_ d: Decimal) -> Double { NSDecimalNumber(decimal: d).doubleValue }5455 /// Value for a report cell.56 public static func value(_ d: Decimal) -> Value { Report.num(double(d)) }57}5859/// A proleptic Gregorian calendar date without time or time zone.60public struct CalendarDate: Sendable, Hashable, Codable, Comparable, CustomStringConvertible {61 public var year: Int62 public var month: Int63 public var day: Int6465 public init(_ year: Int, _ month: Int, _ day: Int) { self.year = year; self.month = month; self.day = day }6667 /// From a spreadsheet date serial (1900 system by default).68 public init(serial: Double, date1904: Bool = false) {69 let c = DateSerial.components(serial, date1904: date1904)70 self.init(c.year, c.month, c.day)71 }7273 /// Days since 1970-01-01.74 public var dayNumber: Int { DateSerial.daysFromCivil(year, month, day) }75 /// Spreadsheet serial of this date.76 public func serial(date1904: Bool = false) -> Double { DateSerial.serial(year: year, month: month, day: day, date1904: date1904) }7778 public init(dayNumber: Int) {79 let c = DateSerial.civilFromDays(dayNumber)80 self.init(c.year, c.month, c.day)81 }8283 public static func < (a: CalendarDate, b: CalendarDate) -> Bool { (a.year, a.month, a.day) < (b.year, b.month, b.day) }8485 /// Actual number of days from `self` to `other` (negative when `other` is earlier).86 public func days(to other: CalendarDate) -> Int { other.dayNumber - dayNumber }8788 public var isLastDayOfMonth: Bool { day == DateSerial.daysInMonth(year: year, month: month) }89 public var isLastDayOfFebruary: Bool { month == 2 && isLastDayOfMonth }9091 /// Adds calendar months. The day is clamped to the length of the target month; with92 /// `endOfMonth` the result is always the last day of its month.93 public func addingMonths(_ n: Int, endOfMonth: Bool = false) -> CalendarDate {94 let total = year * 12 + (month - 1) + n95 let y = Int((Double(total) / 12).rounded(.down)), m = total - y * 12 + 196 let dim = DateSerial.daysInMonth(year: y, month: m)97 return CalendarDate(y, m, endOfMonth ? dim : min(day, dim))98 }99100 public func addingDays(_ n: Int) -> CalendarDate { CalendarDate(dayNumber: dayNumber + n) }101102 /// ISO-8601 "YYYY-MM-DD".103 public var description: String { String(format: "%04d-%02d-%02d", year, month, day) }104}105