如何将数字转换为文字,如口语?

时间:2014-06-11 07:25:40

标签: ios iphone

如果值以百万为单位,则字符串应为.. miliion ..thousand ..hundred。 我使用了这段代码,但它显示了千万的价值,如何解决这个问题?

  //convert to words
 NSString *wordNumber;
 NSNumber *numberValue = [NSNumber numberWithInt:digit]; //needs to be NSNumber!
 NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
 [numberFormatter setNumberStyle:NSNumberFormatterSpellOutStyle];
  //    [numberFormatter setNumberStyle:@"en_IN"];
 [numberFormatter setLocale:[NSLocale currentLocale]]; // happen by default?
 wordNumber = [numberFormatter stringFromNumber:numberValue];
 NSLog(@"Answer: %@", wordNumber);

1 个答案:

答案 0 :(得分:1)

它对我有用。

代码:

#import <Foundation/Foundation.h>

int main(int argc, char *argv[]) {
    @autoreleasepool {
             //convert to words
             NSString *wordNumber;
             NSNumber *numberValue = @6453323; //needs to be NSNumber!
             NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
             [numberFormatter setNumberStyle:NSNumberFormatterSpellOutStyle];
              //    [numberFormatter setNumberStyle:@"en_IN"];
             [numberFormatter setLocale:[NSLocale currentLocale]]; // happen by default?
             wordNumber = [numberFormatter stringFromNumber:numberValue];
             NSLog(@"Answer: %@", wordNumber);
    }
}

输出:

  

2014-06-11 09:51:35.472无标题[480:507]答案:六百四十五万三千三百二十三