概述
二话不说直接上代码,调用就好,谢谢
//金钱每三位加一个逗号
+(NSString *)countNumAndChangeformat:(NSString *)num
{
if([num rangeOfString:@"."].location !=NSNotFound) {
NSString *losttotal = [NSString stringWithFormat:@"%.2f",[num floatValue]];//小数点后只保留两位
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;
}
}
最后
以上就是大意吐司为你收集整理的iOS 金钱价格每三位加一个“,”逗号,小数点省略到两位的全部内容,希望文章能够帮你解决iOS 金钱价格每三位加一个“,”逗号,小数点省略到两位所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复