iOS 数字每隔3位用逗号分隔

需求:spa

1.只有整数,没有小数code

+(NSString *)stringWithCharmCount:(NSString *)charmCount
{
    //将要分割的字符串变为可变字符串
    NSMutableString *countMutStr = [[NSMutableString alloc]initWithString:charmCount];
    //字符串长度
    NSInteger length = countMutStr.length;
    //除数
    NSInteger divisor = length/3;
    //余数
    NSInteger remainder = length%3;
    //有多少个逗号
    NSInteger commaCount;
    if (remainder == 0) {   //当余数为0的时候,除数-1==逗号数量
        commaCount = divisor - 1;
    }else{  //不然 除数==逗号数量
        commaCount = divisor;
    }
    //根据逗号数量,for循环依次添加逗号进行分隔
    for (int i = 1; i<commaCount+1; i++) {
        [countMutStr insertString:@"," atIndex:length - i * 3];
    }
    return countMutStr;
}
相关文章
相关标签/搜索