如何将NSInteger转换为带有印度卢比的objective-c中的相应单词

时间:2017-04-05 05:07:06

标签: ios objective-c nsinteger

NSInteger someNumber = 211242; 

我想知道一些函数,这样就会返回一个类似于&#34的字符串;只有二十一万二千二百四十二卢比"

1 个答案:

答案 0 :(得分:1)

这是解决方案。您必须使用 NSNumberFormatter

    NSInteger anInt = 11242043;
    NSString *wordNumber;

    //convert to words
    NSNumber *numberValue = [NSNumber numberWithInt:anInt]; //needs to be NSNumber!
    NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
    [numberFormatter setNumberStyle:NSNumberFormatterSpellOutStyle];
    wordNumber = [numberFormatter stringFromNumber:numberValue];
    NSLog(@"Answer: %@", wordNumber);

快速解决方案:

extension Int {
    var asWord:String {
        let numberValue = NSNumber(integer: self)
        var formatter = NSNumberFormatter()
        formatter.numberStyle = .SpellOutStyle
        return "\(formatter.stringFromNumber(self)!)"
    }
}