17.9.22 更新...
1. 这个类NSNumberFormatter
NSNumberFormatter *moneyFormatter = [[NSNumberFormatter alloc] init];
moneyFormatter.positiveFormat = @"###,##0.00";
NSString *formatString = [moneyFormatter stringFromNumber:@(1234567.123)];
效果入下图
下边这个方法太麻烦... 直接用上边这个方法吧
2.先取保留俩位小数, 再传入对应 string 传入 111111.09 得到 111,111.09 系统自带的转换方法有时候小数位计算不准确
- (NSString *)separatedDigitStringWithStr:(NSString *)digitString {
if (digitString.length <= 3) {
return digitString;
} else {
NSMutableString *processString = [NSMutableString stringWithString:digitString];
int location = processString.length - 3;
NSMutableArray *processArray = [NSMutableArray array];
while (location >= 0) {
NSString *temp = [processString substringWithRange:NSMakeRange(location, 3)];
[processArray addObject:temp];
if (location < 3 && location > 0) {
NSString *t = [processString substringWithRange:NSMakeRange(0, location)];
[processArray addObject:t];
}
location -= 3;
}
NSMutableArray *resultsArray = [NSMutableArray array];
int k = 0;
for (NSString *str in processArray) {
k++;
NSMutableString *tmp = [NSMutableString stringWithString:str];
if (str.length > 2 && k < processArray.count ) {
[tmp insertString:@"," atIndex:0];
[resultsArray addObject:tmp];
} else {
[resultsArray addObject:tmp];
}
}
NSMutableString *resultString = [NSMutableString string];
for (int i = resultsArray.count - 1 ; i >= 0; i--) {
NSString *tmp = [resultsArray objectAtIndex:i];
[resultString appendString:tmp];
}
NSRange range = NSMakeRange(resultString.length - 4, 1);
[resultString deleteCharactersInRange:range];
return resultString;
}
}