Easiest way to format a number with thousand separators to an NSString according to the Locale

丶灬走出姿态 提交于 2019-11-28 07:20:38
Todd Ransom

For 10.6 this works:

NSNumberFormatter* numberFormatter = [[NSNumberFormatter alloc] init];
[numberFormatter setFormatterBehavior: NSNumberFormatterBehavior10_4];
[numberFormatter setNumberStyle: NSNumberFormatterDecimalStyle];
NSString *numberString = [numberFormatter stringFromNumber: [NSNumber numberWithInteger: i]];

And it properly handles localization.

I have recently discovered this one-liner:

[@1234567 descriptionWithLocale:[NSLocale currentLocale]];  // 1,234,567

Or in Swift 2:

1234567.descriptionWithLocale(NSLocale.currentLocale())     // 1,234,567

Swift 3/4:

(1234567 as NSNumber).description(withLocale: Locale.current)

Formatted per the question:

[@(n) descriptionWithLocale:[NSLocale currentLocale]];

Formatted without Objective-C literals:

[[NSNumber numberWithInt:n] descriptionWithLocale:[NSLocale currentLocale]];

This is the solution I was looking for when I asked the question. Available since iOS 2.0 and OS X 10.0, documented to return a string version of the number formatted as per the locale provided. stringValue is even documented to use this method but passing nil.

Seeing as it is my question and this fits my answer best, I am tempted to change the tick, but it seems cruel. Update I changed the tick, this answer is the answer.

The below doesn't address the locale, but it is a better way (in my opinion) of setting the thousand separator on the number formatter.

NSNumberFormatter *numberFormat = [[[NSNumberFormatter alloc] init] autorelease];
numberFormat.usesGroupingSeparator = YES;
numberFormat.groupingSeparator = @",";
numberFormat.groupingSize = 3;   

Todd Ransom answered this perfectly.

I would just like to add (in a separate comment, so I can show some nicely formatted code), that if you plan to do this regularly, it's worth creating an NSString helper class.

So, create yourself an NSStringHelper.h containing this:

#import <Foundation/Foundation.h>

@interface NSString (NSStringHelper)

+(NSString*)formatWithThousandSeparator:(NSInteger)number;

@end

..and an NSStringHelper.m file containing this:

#import "NSStringHelper.h"

@implementation NSString (NSStringHelper)

+(NSString*)formatWithThousandSeparator:(NSInteger)number
{
    //  Format a number with thousand seperators, eg: "12,345"
    NSNumberFormatter* numberFormatter = [[NSNumberFormatter alloc] init];
    [numberFormatter setFormatterBehavior: NSNumberFormatterBehavior10_4];
    [numberFormatter setNumberStyle: NSNumberFormatterDecimalStyle];
    NSString *result = [numberFormatter stringFromNumber:[NSNumber numberWithInteger:number]];
    return result;
}

@end

This gives you the perfect way to reuse this code in future projects.

 #import "NSStringHelper.h"

 NSInteger numOfUsers = 12345;
 NSString* strNumberOfUsers = [NSString formatWithThousandSeparator:numOfUsers];

Cool, hey ?

Again, apologies for reposting Todd's answer (which was exactly what I was looking for !), but this is a great way to solve the problem, and have it ready to be used in your future XCode projects.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!