Objective-C:将数字格式化为序数:1, 2, 3, .. to 1st, 2nd, 3rd

Objective-C: format numbers to ordinals: 1, 2, 3, .. to 1st, 2nd, 3rd(Objective-C:将数字格式化为序数:1, 2, 3, .. to 1st, 2nd, 3rd)
本文介绍了Objective-C:将数字格式化为序数:1, 2, 3, .. to 1st, 2nd, 3rd的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Objective C 中,有没有办法将 integer 格式化为序数1 => "1st"、2 => "2nd" 等...适用于任何语言?因此,如果用户是法国人,他会看到1er"、2ieme"等.

In Objective C, is there any way to format an integer to ordinals 1 => "1st", 2 => "2nd" etc... that works for any language? So if the user is French he will see "1er", "2ieme" etc..

非常感谢!

编辑:这是一个 iOS 应用程序

Edit: This is for an iOs app

推荐答案

你有没有看过 TTTOrdinalNumberFormatter" rel="noreferrer">FormatterKit?它工作得很好,我很确定这正是您正在寻找的.

Have you taken a look at TTTOrdinalNumberFormatter which is in FormatterKit? It works great, and I'm pretty sure it's exactly what you're looking for.

这是一个从套件中提取的示例:

Here's an example taken from the kit:

TTTOrdinalNumberFormatter *ordinalNumberFormatter = [[TTTOrdinalNumberFormatter alloc] init];
[ordinalNumberFormatter setLocale:[NSLocale currentLocale]];
[ordinalNumberFormatter setGrammaticalGender:TTTOrdinalNumberFormatterMaleGender];
NSNumber *number = [NSNumber numberWithInteger:2];
NSLog(@"%@", [NSString stringWithFormat:NSLocalizedString(@"You came in %@ place!", nil), [ordinalNumberFormatter stringFromNumber:number]]);

假设您为You come in %@ place!"提供了本地化字符串,输出将是:

Assuming you've provided localized strings for "You came in %@ place!", the output would be:

* English: "You came in 2nd place!"
* French: "Vous êtes venu à la 2eme place!"
* Spanish: "Usted llegó en 2.o lugar!"

这篇关于Objective-C:将数字格式化为序数:1, 2, 3, .. to 1st, 2nd, 3rd的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Why local notification is not firing for UNCalendarNotificationTrigger(为什么没有为UNCalendarNotificationTrigger触发本地通知)
iOS VoiceOver functionality changes with Bundle Identifier(IOS画外音功能随捆绑包标识符而变化)
tabbar middle tab out of tabbar corner(选项卡栏中间的选项卡角外)
Pushing UIViewController above UITabBar(将UIView控制器推送到UITabBar上方)
How to target newer versions in .gitlab-ci.yml using auto devops (java 11 instead of 8 and Android 31 instead of 29)(如何在.gitlab-ci.yml中使用自动开发工具(Java 11而不是8,Android 31而不是29)瞄准较新的版本)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)