How to convert Cardinal numbers into Ordinal ones

2019-06-16 03:13发布

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.

标签: c++ mfc ordinals
5条回答
叼着烟拽天下
2楼-- · 2019-06-16 03:49

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.

查看更多
Viruses.
3楼-- · 2019-06-16 03:52

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

查看更多
ゆ 、 Hurt°
4楼-- · 2019-06-16 03:56

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楼-- · 2019-06-16 03:56

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

查看更多
祖国的老花朵
6楼-- · 2019-06-16 04:12

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/

查看更多
登录 后发表回答