如何将数字转换为指示字符串

时间:2013-04-05 10:24:38

标签: objective-c numbers formatter

我只想知道如何将数字1,2或3转换为第一,第二或第三? 任何帮助将不胜感激。

3 个答案:

答案 0 :(得分:5)

试试这个,

- (NSString *)getOrdinalTextFor:(NSInteger)number {

     NSNumber *numberValue = [NSNumber numberWithInteger:number];
     NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
     [numberFormatter setNumberStyle:NSNumberFormatterOrdinalStyle];

     return [numberFormatter stringFromNumber:numberValue];
}

答案 1 :(得分:1)

看到这个可能对你有帮助,

NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
    [formatter setNumberStyle: NSNumberFormatterSpellOutStyle];
    NSString* numberString = [formatter stringFromNumber:[NSNumber numberWithInt: 3]];
    NSLog(@"%@",numberString);

答案 2 :(得分:1)

enter link description hereGitHub

下载Formatter工具包
TTTOrdinalNumberFormatter *ordinalNumberFormatter = [[TTTOrdinalNumberFormatter alloc] init];
[ordinalNumberFormatter setGrammaticalGender:TTTOrdinalNumberFormatterMaleGender];
NSNumber *number = [NSNumber numberWithInteger:2];
NSString *str = [ordinalNumberFormatter stringFromNumber:number];
NSLog(@"%@", str);

阅读此链接,非常有用:Number Formatting