iOS 数字每隔3位添加一个逗号的

+(NSString *)countNumAndChangeformat:(NSString *)num  
{  
    int count = 0;  
    long long int a = num.longLongValue;  
    while (a != 0)  
    {  
        count++;  
        a /= 10;  
    }  
    NSMutableString *string = [NSMutableString stringWithString:num];  
    NSMutableString *newstring = [NSMutableString string];  
    while (count > 3) {  
        count -= 3;  
        NSRange rang = NSMakeRange(string.length - 3, 3);  
        NSString *str = [string substringWithRange:rang];  
        [newstring insertString:str atIndex:0];  
        [newstring insertString:@"," atIndex:0];  
        [string deleteCharactersInRange:rang];  
    }  
    [newstring insertString:string atIndex:0];  
    return newstring;  
}

例:123456789

结果:123,456,789

posted @ 2016-07-13 10:31  小屁孩~  阅读(2770)  评论(0编辑  收藏  举报