为增强数字的可读性,往往会每隔多少个数字使用逗号“,”来做一个间隔,那接下来就让我们看看怎样添加这个“,”呢!!
- (NSString *)changeNumberAndInterval:(NSString *)num
{
int count = 0;
long long int a = num.longLongValue;
while (a != 0)
{
count++;
a /= 10;
}
NSMutableString *string = [NSMutableString stringWithString:num];
NSMutableString *newtring = [NSMutableString string];
while (count > 4) {
count -= 4;
NSRange rang = NSMakeRange(string.length - 4, 4);
NSString *str = [string substringWithRange:rang];
[newtring insertString:str atIndex:0];
[newtring insertString:@"," atIndex:0];
[string deleteCharactersInRange:rang];
}
[newtring insertString:string atIndex:0];
return newtring;
}