2988.32 ——> 2,988.32
2988.00 ——> 2,988
+ (NSString *)priceStrWithPrice:(float)price { NSString *priceStr = [NSString stringWithFormat:@"%.2f", price]; NSString *intStr = [[priceStr componentsSeparatedByString:@"."] firstObject]; NSString *dotStr = [[priceStr componentsSeparatedByString:@"."] lastObject]; NSMutableArray *array = [NSMutableArray array]; while ([intStr length] > 3) { NSString *right = [intStr substringFromIndex:[intStr length] - 3]; NSString *left= [intStr substringToIndex:[intStr length] - 3]; [array insertObject:right atIndex:0]; intStr = left; } [array insertObject:intStr atIndex:0]; NSString *result = [array componentsJoinedByString:@","]; if ([dotStr intValue] > 0) { result = [NSString stringWithFormat:@"%@.%@", result, dotStr]; } return result; }
原文地址:http://8386217.blog.51cto.com/8376217/1592512