I want to convert words into number like Ninety Nine to 99
Second to 2nd
and Two to 2
etc
Does anybody knows how to do that?
I want to convert words into number like Ninety Nine to 99
Second to 2nd
and Two to 2
etc
Does anybody knows how to do that?
There is no API from Apple for this kind of purpose. Lexicographical analysis is hard even if it is just for a single language. Expressing a number as words might be relatively simple, but getting from words to numbers would require dealing with many special cases.
The brute force approach would be to have a database of all the various possible combinations. A smarter approach would probably analyse the individual words and reason what number that could mean.
You are looking for this, NSNumberFormatterStyle. It has an option as NSNumberFormatterSpellOutStyle
.
As per the apple documentation,
NSNumberFormatterSpellOutStyle: Specifies a spell-out format; for example, “23” becomes “twenty-three”.
Looks like you want to convert from Word to Number and not Number to Word. I misread your question.
You might have to manually do that as there are no APIs available from Apple for that. You can check NSNumberFormatter and see if you can find something helpful for this.
As jlehr mentioned, NSLog(@"%@", [formatter numberFromString:@"one hundred forty-three"]);
will give the number 143
. But it may not be a perfect solution which works with all possible combinations.