How to add commas to number every 3 digits in Objective C?

后端 未结 9 702

If I have a number int aNum = 2000000 how do I format this so that I can display it as the NSString 2,000,000?

相关标签:
9条回答
  • 2020-11-28 03:19

    Use NSNumberFormatter.

    Specifically:

    NSNumberFormatter *formatter = [NSNumberFormatter new];
    [formatter setNumberStyle:NSNumberFormatterDecimalStyle]; // this line is important!
    
    NSString *formatted = [formatter stringFromNumber:[NSNumber numberWithInteger:2000000]];
    
    [formatter release];
    

    By default NSNumberFormatter uses the current locale so the grouping separators are set to their correct values by default. The key thing is to remember to set a number style.

    0 讨论(0)
  • 2020-11-28 03:26

    Even easier:

    NSNumber *someNumber = @(1234567890);
    NSString *modelNumberString = [NSString localizedStringWithFormat:@"%@", someNumber];
    NSLog(@"Number with commas: %@", modelNumberString);
    

    coworker just taught me this today. #amazing

    0 讨论(0)
  • 2020-11-28 03:26

    For those who need to do it with strings of numbers and not just integers (I.e. Big Numbers) I made the following macro:

    #define addCommas(__string) (\
    (^NSString *(void){\
    NSString *__numberString = __string;\
    NSString *__integerPortion = __numberString;\
    NSString *__decimalPortion = @"";\
    if ([__string containsString:@"."]) {\
    __integerPortion = [__numberString componentsSeparatedByString:@"."][0];\
    __decimalPortion = st(@".%@", [__numberString componentsSeparatedByString:@"."][1]);\
    }\
    int __i = (int)__integerPortion.length-3;\
    while (__i > 0) {\
    __integerPortion = st(@"%@,%@", substringInRange(__integerPortion, 0, __i), substringInRange(__integerPortion, __i, (int)__integerPortion.length));\
    __i -= 3;\
    }\
    __numberString = st(@"%@%@", __integerPortion, __decimalPortion);\
    return __numberString;\
    })()\
    )
    
    0 讨论(0)
  • 2020-11-28 03:27

    There's a static method on NSNumberFormatter that does just what you need:

    int aNum = 2000000;
    NSString *display = [NSNumberFormatter localizedStringFromNumber:@(aNum)
                                                         numberStyle:NSNumberFormatterDecimalStyle];
    

    This way is a little more succinct than creating a new NSNumberFormatter if you don't need to do any additional configuration of the formatter.

    0 讨论(0)
  • 2020-11-28 03:31

    Swift version

    let formatter = NSNumberFormatter()
    formatter.numberStyle = .DecimalStyle
    formatter.maximumFractionDigits = decimalPlaces
    let result = formatter.stringFromNumber(NSNumber(double: 8.0))
    

    By http://ios.eezytutorials.com

    0 讨论(0)
  • 2020-11-28 03:33

    Don't do your own number formatting. You will almost certainly not get all the edge cases right or correctly handle all possible locales. Use the NSNumberFormatter for formatting numeric data to a localized string representation.

    You would use the NSNumberFormatter instance method -setGroupingSeparator: to set the grouping separator to @"," (or better yet [[NSLocale currentLocale] objectForKey:NSLocaleGroupingSeparator]; thanks @ntesler) and -setGroupingSize: to put a grouping separator every 3 digits.

    0 讨论(0)
提交回复
热议问题