問題描述
在 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的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!