这里主要是用到iOS系统NSNumberFormatter 类,这是一个苹果系统专门用来处理数据精确和各种数据处理格式的类。
通过NSNumberFormatter,可以设置NSNumber输出的格式。例如如下代码:
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
formatter.numberStyle = NSNumberFormatterDecimalStyle;
NSString *string = [formatter stringFromNumber:[NSNumber numberWithInt:123456789]];
NSLog(@"Formatted number string:%@",string);
输出结果为:[1223:403] Formatted number string:123,456,789
更多的类型展示可以关注以下链接
https://blog.csdn.net/liufangbaishi2014/article/details/50344781
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(100, 100, 300, 30)];
label.text= [self positiveFormat:@"2345688.0009"];
[self.view addSubview:label];
-(NSString*)positiveFormat:(NSString*)text{
if(!text || [text floatValue] ==0){
return@"0.00";
}else{
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
[numberFormatter setPositiveFormat:@",###.00;"];
return [numberFormatter stringFromNumber:[NSNumber numberWithDouble:[text doubleValue]]];
}
return @"";
}
网友评论