// What a cheque writes beside its figures: the amount in words, // as a hand writes it on an Indian cheque ("Two thousand three hundred and // forty only"), counted in the Indian way — crore, lakh, thousand, hundred — // with "and" before the last tens as British English says it, the tens // joined by a hyphen ("twenty-four"), paise after the rupees ("… rupees and // fifty paise only"), and "only" closing it so nothing can be written after. // And the figures a period object types: an amount the core said // ("₹1,24,500", "₹16,01,625.50", "−₹500") read back into its rupees and // paise, and typed with its two places ("1,24,500.00"), grouped as the core // grouped it. // // The cheque's writing (`TransactionChequeFace`) and the figures every money // object types: composed from the card's amount, which the core says and the // kit does not spell out in words. import Foundation import CottonValues public enum RupeeWords { private static let ones = [ "", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen", ] private static let tens = ["", "", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"] /// `rupees` and `paise` in words, opening on a capital and closing on /// "only": "Two thousand three hundred and forty only", "One lakh /// twenty-four thousand five hundred only", "Twelve rupees and fifty /// paise only", "Fifty paise only", "Nil only" for nought. Paise are /// 0 to 99; a negative amount is written as its size. public static func written(rupees: Int, paise: Int = 0) -> String { let rupees = rupees.magnitude let paise = min(99, paise.magnitude) let said: String switch (rupees, paise) { case (0, 0): said = "nil" case (_, 0): said = words(rupees) case (0, _): said = "\(words(paise)) paise" default: said = ShellLines.line("cards", "faces.rupees_and_paise", ["rupees": words(rupees), "paise": words(paise)]) } return said.prefix(1).uppercased() + said.dropFirst() + " only" } /// A whole number in words, the Indian way: crore, lakh, thousand, /// hundred, and "and" before the last tens where anything stands /// before them. Nought is the empty string. static func words(_ number: UInt) -> String { var parts: [String] = [] var rest = number let crore = rest / 10_000_000 rest %= 10_000_000 if crore > 0 { parts.append("\(words(crore)) crore") } for (size, name) in [(UInt(100_000), "lakh"), (1_000, "thousand"), (100, "hundred")] { let count = rest / size rest %= size if count > 0 { parts.append("\(below100(count)) \(name)") } } if rest > 0 { if !parts.isEmpty { parts.append("and") } parts.append(below100(rest)) } return parts.joined(separator: " ") } private static func below100(_ number: UInt) -> String { if number < 20 { return ones[Int(number)] } let unit = ones[Int(number % 10)] return unit.isEmpty ? tens[Int(number / 10)] : "\(tens[Int(number / 10)])-\(unit)" } } /// An amount as the core said it, read back into its parts: the mark before /// the figures ("₹", "$", "SGD"), its whole units as grouped, and its minor /// part; nil when the words hold no figure. public struct SaidAmount: Equatable, Sendable { /// The currency's mark as said ("₹"); empty when the core said none. public var mark: String /// The whole units' figures as grouped ("1,24,500"). public var grouped: String /// The whole units. public var units: Int /// The minor part's digits as said ("50"), empty when it said none. public var minor: String /// The amount is below nought ("−₹500"). public var negative: Bool /// The rupee's mark, as the core's money set says it. public static let rupee = "\u{20B9}" public init?(_ said: String) { let trimmed = said.trimmingCharacters(in: .whitespaces) negative = trimmed.hasPrefix("\u{2212}") || trimmed.hasPrefix("-") guard let first = trimmed.firstIndex(where: \.isNumber) else { return nil } let figures = trimmed[first...].prefix { $0.isNumber || $0 == "," || $0 == "." } mark = String(trimmed[.. 1 ? String(halves[1]) : "" guard let units = Int(grouped.filter(\.isNumber)) else { return nil } self.units = units } /// In rupees: the mark is the rupee's. public var isRupees: Bool { mark == Self.rupee } /// The minor part as a number of hundredths (paise), "5" read as 50. public var hundredths: Int { guard !minor.isEmpty else { return 0 } let two = (minor + "00").prefix(2) return Int(two) ?? 0 } /// The figures typed with two places, as a period object types them /// ("1,24,500.00"), a minus before them below nought. public var typed: String { let places = minor.isEmpty ? "00" : minor return (negative ? "\u{2212}" : "") + grouped + "." + places } /// The amount in hundredths, signed: what two amounts are compared by. public var signedHundredths: Int { let size = units * 100 + hundredths return negative ? -size : size } }