在iOS开发中,和货币价格计算相关的,需要注意计算精度的问题。即使只是两位小数,也会出现误差。使用float类型运算,是完全不够的。所以我在网上来找寻答案,但是在百度找了好久,没有发现一个好的解决方案,后来发现了NSDecimalNumber这个类,但是国内搜索引擎上的资料用太少了。
先叙述下我遇到的问题,我的服务器传给我的是一个float的值,作为一个对外的库,由于存在版本延续,需要保留对外的flaot的类型,不改变API,选择进行内部适配。
问题引出
float a =0.01;
int b =99999999;
double c =0.0;
c = a*b;NSLog(@"%f",c);
//输出结果为 1000000.000000
NSLog(@"%.2f",c);
//输出结果为 1000000.00
//明显不够精确
在网上找到了一个国内朋友的博客也遇到和我一样的问题,他尝试了如下两种解决方案
将float强制转换为double
c = a*(double)b;NSLog(@"%f",c);//输出结果 999999.967648NSLog(@"%.2f",c);//输出结果 999999.97// 明显已经丢失精度
通过和NSString的转换,将计算的原始数据转换为纯粹的double类型的数据,这样的计算精度就可以达到要求了
NSString *objA = [NSString stringWithFormat:@"%.2f", a];NSString *objB = [NSString stringWithFormat:@"%.2f", (double)b];c = [objA doubleValue] * [objB doubleValue];NSLog(@"%.2f",c);//输出结果 999999.99
最终方案
NSString *decimalNumberMutiplyWithString(NSString *multiplierValue,NSString *multiplicandValue){
NSDecimalNumber *multiplierNumber = [NSDecimalNumber decimalNumberWithString:multiplierValue];
NSDecimalNumber *multiplicandNumber = [NSDecimalNumber decimalNumberWithString:multiplicandValue];
NSDecimalNumber *product = [multiplicandNumber decimalNumberByMultiplyingBy:multiplierNumber];
return [product stringValue];
}
NSLog(@"%@",decimalNumberMutiplyWithString([NSString stringWithFormat:@"%f",a], [NSString stringWithFormat:@"%d",b]));//输出结果 999999.99
NSDecimalNumber针对 float double 精度丢失问题(解决方法)
NSDecimalNumberHandler *roundingBehavior = [NSDecimalNumberHandler decimalNumberHandlerWithRoundingMode:NSRoundPlain scale:6 raiseOnExactness:NO raiseOnOverflow:NO raiseOnUnderflow:NO raiseOnDivideByZero:NO];
NSDecimalNumber *resultWalletAmt = [self.walletAmt decimalNumberByRoundingAccordingToBehavior:roundingBehavior];
self.txtAmt.txtContent.text= [[resultWalletAmtdecimalNumberBySubtracting:gas]stringValue];
NSDecimalNumber 比较大小
NSDecimalNumber *change = [NSDecimalNumber decimalNumberWithString:[NSString stringWithFormat:@"%0.3f", [[coin.today change] floatValue]]];
if(change){//![coin.today.last isEqualToString:coin.today.open] &&
NSComparisonResultresult = [changecompare:@(0)];
if(result ==NSOrderedAscending){
self.txtPrice.textColor=self.changeBgView.backgroundColor=kHex(@"E41856");
self.txtChange.text= [NSStringstringWithFormat:@"%@%@%%",@"",change];
}elseif(result ==NSOrderedDescending){
self.txtPrice.textColor=self.changeBgView.backgroundColor=kHex(@"00A97E");
self.txtChange.text= [NSStringstringWithFormat:@"%@%@%%",@"+",change];
}else{
self.txtPrice.textColor= [UIColorwhiteColor];
self.changeBgView.backgroundColor=kHex(@"00A97E");
self.txtChange.text=@"+0.00%";
}
}else{
self.txtPrice.textColor = [UIColor whiteColor];
self.changeBgView.backgroundColor=kHex(@"00A97E");
self.txtChange.text=@"+0.00%";
}
四舍五入
// 四舍五入NSDecimalNumberHandler*roundPlain = [NSDecimalNumberHandlerdecimalNumberHandlerWithRoundingMode:NSRoundPlainscale:2raiseOnExactness:NOraiseOnOverflow:NOraiseOnUnderflow:NOraiseOnDivideByZero:YES]; {NSString*addStr =@"1.0";NSString*oneStr =@"1.114";NSString*twoStr =@"1.116";NSDecimalNumber*oneNum = [NSDecimalNumberdecimalNumberWithString:oneStr];NSDecimalNumber*twoNum = [NSDecimalNumberdecimalNumberWithString:twoStr];NSDecimalNumber*addNum = [NSDecimalNumberdecimalNumberWithString:addStr];// 加法oneNum = [oneNum decimalNumberByAdding:addNum withBehavior:roundPlain]; twoNum = [twoNum decimalNumberByAdding:addNum withBehavior:roundPlain];NSLog(@"\n1.114 + 1.0 (保留两位小数)四舍五入的结果为:%@\n1.116 + 1.0 (保留两位小数)四舍五入的结果为:%@", oneNum, twoNum); }