Created
July 15, 2019 19:15
-
-
Save niazoff/17522efe04cf1654dacf207e34885b1d to your computer and use it in GitHub Desktop.
Hebrew Numerals in Swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
extension Int { | |
/// Returns the Hebrew numeral represented by the receiver's value. | |
/// | |
/// Example: A value of 1 retuns א׳. | |
/// A value of 0 or any value above one million returns the value in regular Arabic numerals. | |
func toHebrewNumeral() -> String { | |
let absoluteSelf = abs(self) | |
// `DateFormatter` won't use Hebrew numerals after a million so simply return self. | |
guard absoluteSelf != 0 && absoluteSelf <= 1_000_000 else { return String(self) } | |
let calendar = Calendar(identifier: .hebrew) | |
// Create a date with the year set to self in the Hebrew calendar. | |
guard let date = calendar.date(from: .init(year: self)) else { return String(self) } | |
let formatter = DateFormatter() | |
formatter.calendar = calendar | |
formatter.locale = Locale(identifier: "he") | |
formatter.dateStyle = .short | |
let dateString = formatter.string(from: date) | |
// The year substrings that reflect self come after the first two components. | |
// Multiple year substrings occur when the year includes a space as in "אלף אלפים". | |
let yearSubstrings = dateString.split(separator: " ")[2...] | |
return yearSubstrings.joined(separator: " ") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Cheers to @MosheBerman for the inspiration.