How to convert Cardinal numbers into Ordinal ones

2019-06-16 03:32发布

问题:

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.

回答1:

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).



回答2:

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.



回答3:

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/



回答4:

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



回答5:

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



标签: c++ mfc ordinals