5

There are few ways to generate short ordinal numbers like "1st", "2nd", etc, with NSNumberFormatter (from iOS 9) or 3rd parties like FormatterKit.

There is also a way to spell out cardinal numbers with NSNumberFormatter that gives one, two, three.

However, I need to get the unabbreviated spelling - first, second, third.

I know that iOS internally can do it, because if I pass "1st" to AVSpeechUtterance it pronounces it as "first". I was wondering if there is any API to get a string with spelling of ordinal numbers, preferably, for multiple languages?

Dmitry
  • 2,837
  • 1
  • 30
  • 48
  • could you just take nsnumberformatterspelloutstyle and modify the last word? There is a solution like that here: http://stackoverflow.com/questions/6716596/is-there-an-objective-c-method-that-takes-a-number-and-spells-it-out – Jon Rose Jan 23 '17 at 11:31
  • For english only it's ok, but this logic doesn't work for other languages. That's why I'd prefer to find an API that could format for any `Locale` – Dmitry Jan 23 '17 at 11:34
  • If only `spellOut` was a flag in `NSNumberFormatter` instead of being part of `numberStyle`, so it would be possible to specify `.ordinal` and `.spellOut` – Dmitry Jan 23 '17 at 11:36
  • So far I found that ICU4C can do it - http://userguide.icu-project.org/formatparse/numbers. ICU is also part of iOS, however, the required `rbnf.h` is not part of the public headers. Trying to find an easy way to integrate it with my iOS project – Dmitry Jan 23 '17 at 12:47
  • I found that `CFNumberFormatter` uses ICU4C. I also managed to link to the ICU4C and get the required results from there. I'm thinking of extending `CFNumberFormatter` and subsequently `NumberFormatter` in Swift. If anyone is interested in this, I'll publish a cocoapod with the results – Dmitry Jan 24 '17 at 11:52
  • does icu have support for other languages besides English? – Jon Rose Jan 24 '17 at 12:55
  • yes, it does. As you can see from the source code of `CFNumberFormatter`, it goes straight into ICU. So it has all languages that are in `NumberFormatter`. So far I have tested English, Portuguese, Chinese, Russian, Japanese, Italian, German, Spanish and French. It even supports Esperanto :) – Dmitry Jan 24 '17 at 14:50
  • Also for some languages it gives options for different spellings. For example, it could be feminine or masculine ordinals for Spanish/French/Portugues. In some languages it can also do singular/plural. It has a separate mode for spelling years too. Russian language has 30 different modes for spelling. – Dmitry Jan 24 '17 at 14:54

1 Answers1

2

I couldn't find an existing libraries for that, so I created my own.

It uses ICU4C underneath from the system libraries. Before using, please check how the example project works:

https://github.com/dimat/DMNumberSpellOutFormatter

Dmitry
  • 2,837
  • 1
  • 30
  • 48