字符串插入逗号

来源:互联网 发布:linux运维面试题及答案 编辑:程序博客网 时间:2024/05/20 01:13
- (NSString *)addSeparatorForPriceString:(NSString *)priceStr{    NSMutableString *tempStr = priceStr.mutableCopy;    NSRange range = [priceStr rangeOfString:@"."];//如果存在小数点,index就是小数点的位置    NSInteger index = 0;    if (range.length > 0) {        index = range.location;    }else    {        index = priceStr.length;//否则index就是输入数字的长度    }        while ((index - 3) > 0) {        index -= 3; //循环计算,三位插入一个逗号        [tempStr insertString:@"," atIndex:index];    }    tempStr = [tempStr stringByReplacingOccurrencesOfString:@"." withString:@","].mutableCopy;//将圆点替换成逗号    return tempStr;}

原创粉丝点击