将价钱(float)转换成带分隔符的价钱(字符串)
发表于:2024-11-28 作者:热门IT资讯网编辑
编辑最后更新 2024年11月28日,2988.32 --> 2,988.322988.00 --> 2,988+ (NSString *)priceStrWithPrice:(float)price{ NSString *pric
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;}