How to convert Cardinal numbers into Ordinal ones
Asked Answered
O

5

9

Is there an easy way to convert the number 1, 2, 3, ... to "1st", "2nd", "3rd", ..., and in such a way that I can give the function a language and have it return me the correct form for the language I'm targeting? Either standard C++ (stl or boost OK), MFC or ATL, win32 api or a small, single-purpose and free library that I can download from somewhere. Thanks.

Odele answered 25/9, 2008 at 20:45 Comment(0)
K
5

I doubt whether it is possible at all, since in many languages this form will depend on the context, like gender or case of the noun it describes and different languages will require different kind of context information to allow to determine the correct form.

EDIT: E.g. in Polish it is "5-ta klasa" (5th class) vs. "5-ty miesiąc" (5th month) vs. "w 5-tym miesiącu" (in the 5th month).

Kostroma answered 30/9, 2008 at 7:12 Comment(3)
Do you have any specific languages or examples in mind? AFAIK, but my practical experience is limited to Western European languages, the ordinal (1st, 2nd) is always the same, and only the fully spelled out form is conjugated (eg el secundo mano/ la secunda dia in Spanish).Odele
Interesting, thanks for that. It looks like I will be using the ICU library to get ordinals but I don't know how they handle this. I'll test that out.Odele
@Roel: It's "el segundo día/la segunda mano" and they are different even when not fully spelled out: el 2º día/la 2ª manoPhillips
O
0

I've spend quite some time researching this, because it's too large a project to get right myself. It looks like the ICU library is the only one that provides this functionality in a somewhat comprehensive way (http://www.icu-project.org/apiref/icu4c/classRuleBasedNumberFormat.html). I'm not too keen on incorporating a huge library like that, though. I'll keep on looking and I'm still open to suggestions.

Odele answered 26/9, 2008 at 7:37 Comment(0)
A
0

Did you look up the CLDR repository on the Unicode site? I don't know if they have this kind of thing but since it's probably the most comprehensive locale data repository out there, it's probably worth a look. http://www.unicode.org/cldr/

Aletheaalethia answered 2/10, 2008 at 6:58 Comment(1)
Yes, CLDR has ordinal information and ICU uses it as a data source. I'd have to write my own code around it though, or split the ordinal-related code from ICU into a separate sub-library or so. Both not very attractive.Odele
C
0

Since you use C++, I assume you could use GNU gettext (there's a Windows port as well) for all the translations, or at least get the idea how they solved it. Here's the relevant manual page on plural forms that explains the problem (which you already found, but in more detail) and their solution:

http://www.gnu.org/software/automake/manual/gettext/Plural-forms.html

Calore answered 7/10, 2008 at 19:19 Comment(1)
The only functionality I could find in gettext is for plurals, not ordinals. I could build my own solution using something similar to their approach but I don't want that; I'd need access to many native speakers. I'm not that motivated :) Besides, gettext is GPL which makes it unsuitable for my use.Odele
S
-2

Here is the piece of code on CodeProject that does the job. Haven't tried it on my own.

Sprat answered 26/9, 2008 at 4:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.