本文主要是介绍金+(NSString *)countNumAndChangeformat:(NSString *)num { if([num rangeOfString:@.].l钱数字每三位加一个逗号,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
开发中经常会遇见金钱数字需要每三位添加一个逗号,方便用户读取数量,例:999,9.012,344
调用下面方法即可:
+(NSString *)countNumAndChangeformat:(NSString *)num
{if([num rangeOfString:@"."].location !=NSNotFound) {NSString *losttotal = [NSString stringWithFormat:@"%.2f",[num doubleValue]];//小数点后只保留两位NSArray *array = [losttotal componentsSeparatedByString:@"."];//小数点前:array[0]//小数点后:array[1]int count = 0;num = array[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];NSMutableString *newString = [NSMutableString string];newString =[NSMutableString stringWithFormat:@"%@.%@",newstring,array[1]];return newString;}else {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;}
}
这篇关于金+(NSString *)countNumAndChangeformat:(NSString *)num { if([num rangeOfString:@.].l钱数字每三位加一个逗号的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!