gpt4 book ai didi

ios - NSNumberFormatter 和 'th' 'st' 'nd' 'rd'(序数)数字结尾

转载 作者:IT王子 更新时间:2023-10-29 07:47:55 26 4
gpt4 key购买 nike

有没有办法使用 NSNumberFormatter 来获取 'th' 'st' 'nd' 'rd' 数字结尾?

编辑:

看起来它不存在。这是我正在使用的。

+(NSString*)ordinalNumberFormat:(NSInteger)num{
NSString *ending;

int ones = num % 10;
int tens = floor(num / 10);
tens = tens % 10;
if(tens == 1){
ending = @"th";
}else {
switch (ones) {
case 1:
ending = @"st";
break;
case 2:
ending = @"nd";
break;
case 3:
ending = @"rd";
break;
default:
ending = @"th";
break;
}
}
return [NSString stringWithFormat:@"%d%@", num, ending];
}

改编自 nickf 的回答 Is there an easy way in .NET to get "st", "nd", "rd" and "th" endings for numbers?

最佳答案

从 iOS 9 开始,正确的做法是:

NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
numberFormatter.numberStyle = NSNumberFormatterOrdinalStyle;

NSLog(@"%@", [numberFormatter stringFromNumber:@(1)]); // 1st
NSLog(@"%@", [numberFormatter stringFromNumber:@(2)]); // 2nd
NSLog(@"%@", [numberFormatter stringFromNumber:@(3)]); // 3rd, etc.

或者:

NSLog(@"%@", [NSString localizedStringFromNumber:@(1)
numberStyle:NSNumberFormatterOrdinalStyle]); // 1st

关于ios - NSNumberFormatter 和 'th' 'st' 'nd' 'rd'(序数)数字结尾,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3312935/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com