由于iOS API的原因,接口解析數(shù)據(jù)是存在一個(gè)缺陷,下面將具體講述。
1、問題描述##
如果后臺所傳數(shù)據(jù)是Decimal類型,則解析出的數(shù)據(jù)在精確度上會存在一定的問題。
{
Environment = Both;
"Ground_indoor" = Finished;
"Ground_outdoor" = Finished;
"Max_Working_Height" = "9.800000000000001";
Name = "8m Battery Scissor Lift - Genie GS2632";
"Non_Marking_Tyres" = 1;
"Platform_Length" = "2.26";
"Platform_Type" = "Scissor Lift";
"Platform_Width" = "0.8100000000000001";
"Tax_Class" = E; Weight = 1956;
}
可以發(fā)現(xiàn)這段數(shù)據(jù)里有兩個(gè)數(shù)據(jù)出現(xiàn)了問題,其實(shí)接口傳過來的只是“9.8”和“0.81”。
2、問題解釋##
Or if that is too much to read, let's simply put it this way - for example, let's take the value 0.81 as an example. The number cannot be accurately represented by a float and is rounded up to 0.8100000000000001 because an int value of 81 is represented by the binary value 1010001. In order to make the value 0.81 it would be accurate if you could take 81 x 10^-2 (= 81 / 10^2.) But that’s impossible because you must use the base 2 instead of 10. So the closest to 10^2 = 100 would be 128 = 2^7. The total number of bits you need is 9 : 6 for the value 81 (1010001) + 3 bits for the value 7 (111). Then the value 81 x 2^-7 is not seriously inaccurate. So to improve this inaccuracy, we could change the value 81 and 7 to something else. So the formula for your value would be X = A x 2^B where A and B are integer values positive or negative. The higher the numbers are the higher accuracy become. However as you know the number of bits to represent the values A and B are limited. For float you have a total number of 32. Double has 64 and Decimal has 128.
以上是比較完整的解答,簡而言之就是iOS將接口數(shù)據(jù)先解析為二進(jìn)制數(shù),然后再轉(zhuǎn)成float展示。特別是針對小數(shù)會出現(xiàn)這種問題。
3、解決方案##
- 將接口數(shù)據(jù)以字符串的形式傳給移動(dòng)端,這種方法是最有效的。
NSNumberFormatter *fmt = [[NSNumberFormatter alloc] init];
[fmt setPositiveFormat:@"0.##"];
NSLog(@"%@", [fmt stringFromNumber:dataDict[@"Total"]]);
這種方法相對繁瑣一點(diǎn)。
結(jié)語#
關(guān)于這點(diǎn)問題,很希望有經(jīng)驗(yàn)的大神能出來交流一下。謝謝。