NSFormatter's spellout
method lets you convert a word into a number. NSString's stringByReplacingOccurrencesOfString
lets you convert strings such as thousand into 1000. And you can pick integers out of strings using NSCharacterSet
. However, I am struggling with how to convert a mixture of numbers and strings, for example, about 2.4 million
or the comes to 5 hundred
into a number. The problem is isolating the '2.4 million' from the 'about' or other text.
Applying spellout to "2.4 million" yields 2400000. However, applying it to "about 2.4 million" gives an error.
extension NSString {
public var asNum: NSNumber {
// let stringValue = String(value: self)
let stringValue = self
let formatter = NumberFormatter()
formatter.isLenient = true
formatter.numberStyle = .spellOut
return formatter.number(from: stringValue as String) ?? -1
}
}
How can I isolate just the terms that are part of a valid number?