NSInteger someNumber = 211242;
我想知道一些函数,这样就会返回一个类似于&#34的字符串;只有二十一万二千二百四十二卢比"
答案 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)!)"
}
}