开发遇到的一个坑。在与后台交互的时候,协议规定的数据类型是浮点型,这就要求我们将用户输入的字符串转化为float类型。但是在转化过程中会出现失真的情况。
其实float与str互转会出现失真的情况,原因很简单。这是因为浮点数的存储方式的缘故。
NSLog(@"%f",[@"20.5" floatValue]);
NSLog(@"%f",[@"20.7" floatValue]);
NSLog(@"%f",[@"20.75" floatValue]);
NSLog(@"%f",[@"20.76" floatValue]);
NSLog(@"%f",[@"20.875" floatValue]);
NSLog(@"%f",[@"20.878" floatValue]);
NSLog(@"%f",[@"20.09375" floatValue]);
NSLog(@"%f",[@"20.09335" floatValue]);
NSLog(@"%f",[@"20.546875" floatValue]);
NSLog(@"%f",[@"20.546878" floatValue]);
2018-08-25 11:21:58.833098+0800 floatValueDistortion[3523:408253] 20.500000
2018-08-25 11:21:58.833285+0800 floatValueDistortion[3523:408253] 20.700001
2018-08-25 11:21:58.833401+0800 floatValueDistortion[3523:408253] 20.750000
2018-08-25 11:21:58.833520+0800 floatValueDistortion[3523:408253] 20.760000
2018-08-25 11:21:58.833623+0800 floatValueDistortion[3523:408253] 20.875000
2018-08-25 11:21:58.834162+0800 floatValueDistortion[3523:408253] 20.878000
2018-08-25 11:21:58.834477+0800 floatValueDistortion[3523:408253] 20.093750
2018-08-25 11:21:58.834636+0800 floatValueDistortion[3523:408253] 20.093349
2018-08-25 11:21:58.834784+0800 floatValueDistortion[3523:408253] 20.546875
2018-08-25 11:21:58.834978+0800 floatValueDistortion[3523:408253] 20.546879
在回顾了浮点数的存储方式之后,和后台商议使用更改数据类型为字符串进行传输,问题就解决了。但是如果要用输入的类型进行计算呢?这个时候问题就避不开了。
其实苹果已有线程的API是可以支持的。NSDecimalNumber看定义就可以发现,不同于浮点数的存储,他是十进制的。同时也可以定制精度的取正类型:向上取正、向下去正、四舍五入等。相对与浮点类型的计算,NSDecimalNumber提供了更加精准的计算。
NSDecimalNumber
An object for representing and performing arithmetic on base-10 numbers.
- (void)caculateTest
{
NSString * floatStr1 =@"14.326";
NSString * floatStr2 =@"7.54";
NSDecimalNumber * num1 = [NSDecimalNumber decimalNumberWithString:floatStr1];
NSDecimalNumber * num2 = [NSDecimalNumber decimalNumberWithString:floatStr2];
NSDecimalNumber *num3 = [num1 decimalNumberByAdding:num2];
CGFloat float3 = [floatStr1 floatValue] + [floatStr2 floatValue];
NSLog(@"%@,%f",num3,float3);
NSDecimalNumber *num4 = [num1 decimalNumberBySubtracting:num2];
CGFloat float4 = [floatStr1 floatValue] - [floatStr2 floatValue];
NSLog(@"%@,%f",num4,float4);
NSDecimalNumber *num5 = [num1 decimalNumberByMultiplyingBy:num2];
CGFloat float5 = [floatStr1 floatValue] * [floatStr2 floatValue];
NSLog(@"%@,%f",num5,float5);
NSDecimalNumber *num6 = [num1 decimalNumberByDividingBy:num2];
CGFloat float6 = [floatStr1 floatValue] / [floatStr2 floatValue];
NSLog(@"%@,%f",num6,float6);
}
floatValueDistortion[3897:448522] 21.866,21.866001
2018-08-25 12:31:09.503985+0800 floatValueDistortion[3897:448522] 6.786,6.786000
2018-08-25 12:31:09.504800+0800 floatValueDistortion[3897:448522] 108.01804,108.018044
2018-08-25 12:31:09.504993+0800 floatValueDistortion[3897:448522] 1.9,1.900000
网友评论