iOS介面資料解析問題

weixin_33670713發表於2016-11-10

由於iOS API的原因,介面解析資料是存在一個缺陷,下面將具體講述。

1、問題描述##

如果後臺所傳資料是Decimal型別,則解析出的資料在精確度上會存在一定的問題。

{ 
  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;
}

可以發現這段資料裡有兩個資料出現了問題,其實介面傳過來的只是“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將介面資料先解析為二進位制數,然後再轉成float展示。特別是針對小數會出現這種問題。

3、解決方案##

  • 將介面資料以字串的形式傳給移動端,這種方法是最有效的。
NSNumberFormatter *fmt = [[NSNumberFormatter alloc] init];
[fmt setPositiveFormat:@"0.##"];
NSLog(@"%@", [fmt stringFromNumber:dataDict[@"Total"]]);

這種方法相對繁瑣一點。

結語#

關於這點問題,很希望有經驗的大神能出來交流一下。謝謝。

相關文章