使用NSNumberFormatter:
NSNumberFormatter *numberFormatter = [NSNumberFormatter new];
[numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
NSString *commaString = [numberFormatter stringFromNumber:[NSNumber numberWithInteger:yourIntegerValue]];
NSLog(@"---> %@",commaString);
或通过字符串操作 <This is not the best way>
:
NSMutableString *mutString=[NSMutableString stringWithString:@"123456789"];
NSMutableArray *array=[NSMutableArray new];
for (NSInteger i=1; i<=mutString.length/3; i++) {
NSInteger index=[mutString length]-i*3;
[array insertObject:[mutString substringWithRange:NSMakeRange(index, 3)] atIndex:0];
}
if (mutString.length!=mutString.length/3*3) {
[array insertObject:[mutString substringWithRange:NSMakeRange(0, mutString.length%3)] atIndex:0];
}
NSString *commaString=[array componentsJoinedByString:@","];
NSLog(@"--> %@",commaString);