SwiftExtension // Add i18n number formatters.

This commit is contained in:
ShikiSuen 2022-12-22 16:15:33 +08:00
parent c11e08f33d
commit 228160a62b
1 changed files with 20 additions and 0 deletions

View File

@ -185,3 +185,23 @@ extension String {
extension String {
public var withEllipsis: String { self + "" }
}
// MARK: - Localized String Extension for Integers and Floats
extension BinaryFloatingPoint {
public func i18n(loc: String) -> String {
let formatter = NumberFormatter()
formatter.locale = Locale(identifier: loc)
formatter.numberStyle = .spellOut
return formatter.string(from: NSDecimalNumber(string: "\(self)")) ?? ""
}
}
extension BinaryInteger {
public func i18n(loc: String) -> String {
let formatter = NumberFormatter()
formatter.locale = Locale(identifier: loc)
formatter.numberStyle = .spellOut
return formatter.string(from: NSDecimalNumber(string: "\(self)")) ?? ""
}
}