// 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.importFoundationimportCottonValuespublicenumRupeeWords {privatestaticlet ones = ["", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten","eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen", ]privatestaticlet 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.publicstaticfunc written(rupees: Int, paise: Int = 0) -> String {let rupees = rupees.magnitudelet paise = min(99, paise.magnitude)let said: Stringswitch (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.staticfunc words(_ number: UInt) -> String {var parts: [String] = []var rest = numberlet crore = rest / 10_000_000 rest %= 10_000_000if 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 %= sizeif count > 0 { parts.append("\(below100(count)) \(name)") } }if rest > 0 {if !parts.isEmpty { parts.append("and") } parts.append(below100(rest)) }return parts.joined(separator: " ") }privatestaticfunc 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.publicstructSaidAmount: Equatable, Sendable {/// The currency's mark as said ("₹"); empty when the core said none.publicvar mark: String/// The whole units' figures as grouped ("1,24,500").publicvar grouped: String/// The whole units.publicvar units: Int/// The minor part's digits as said ("50"), empty when it said none.publicvar minor: String/// The amount is below nought ("−₹500").publicvar negative: Bool/// The rupee's mark, as the core's money set says it.publicstaticlet rupee = "\u{20B9}"publicinit?(_ said: String) {let trimmed = said.trimmingCharacters(in: .whitespaces) negative = trimmed.hasPrefix("\u{2212}") || trimmed.hasPrefix("-")guardlet first = trimmed.firstIndex(where: \.isNumber) else { returnnil }let figures = trimmed[first...].prefix { $0.isNumber || $0 == "," || $0 == "." } mark = String(trimmed[..<first]).trimmingCharacters(in: CharacterSet(charactersIn: "\u{2212}-+ "))let halves = figures.split(separator: ".", maxSplits: 1, omittingEmptySubsequences: false) grouped = String(halves[0]) minor = halves.count > 1 ? String(halves[1]) : ""guardlet units = Int(grouped.filter(\.isNumber)) else { returnnil }self.units = units }/// In rupees: the mark is the rupee's.publicvar isRupees: Bool { mark == Self.rupee }/// The minor part as a number of hundredths (paise), "5" read as 50.publicvar hundredths: Int {guard !minor.isEmpty else { return0 }let two = (minor + "00").prefix(2)returnInt(two) ?? 0 }/// The figures typed with two places, as a period object types them/// ("1,24,500.00"), a minus before them below nought.publicvar typed: String {let places = minor.isEmpty ? "00" : minorreturn (negative ? "\u{2212}" : "") + grouped + "." + places }/// The amount in hundredths, signed: what two amounts are compared by.publicvar signedHundredths: Int {let size = units * 100 + hundredthsreturn negative ? -size : size }}