Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

objectFromJSONString has a bug: "priceLogTrend": -9.3,--->priceLogTrend = "-9.300000000000001"; #110

Closed
evoloyeu opened this issue Dec 4, 2012 · 4 comments
Assignees

Comments

@evoloyeu
Copy link

evoloyeu commented Dec 4, 2012

json string:
"priceLogTrend": -9.3,

parsed result:
priceLogTrend = "-9.300000000000001";

the expected result :
priceLogTrend = -9.3;

@evoloyeu
Copy link
Author

evoloyeu commented Dec 5, 2012

numbers are parsed to strings

and some number is parsed with weird error.

like :

"priceLogTrend": -9.3,--->priceLogTrend = "-9.300000000000001";

what I want is "priceLogTrend": -9.3, ---> priceLogTrend = -9.3;

@ghost ghost assigned johnezang Dec 5, 2012
@johnezang
Copy link
Owner

This is not a bug. The "problem" is due to the fact that the value 9.3 can not be represented exactly using a IEEE 754 floating point number.

From the README.md documentation:

The C double primitive type, or IEEE 754 Double 64-bit floating-point, is used to represent floating-point JSON Number values. JSON that contains floating-point Number values that can not be represented as a double (i.e., due to over or underflow) will fail to parse and optionally return a NSError object. The function strtod() is used to perform the conversion. Note that the JSON standard does not allow for infinities or NaN (Not a Number). The conversion and manipulation of floating-point values is non-trivial. Unfortunately, RFC 4627 is silent on how such details should be handled. You should not depend on or expect that when a floating-point value is round tripped that it will have the same textual representation or even compare equal. This is true even when JSONKit is used as both the parser and creator of the JSON, let alone when transferring JSON between different systems and implementations.

@icoco
Copy link

icoco commented Mar 31, 2016

my solution is :

  1. convert the double value to NSString
  2. convert the string to NSDecimalNumber but NSNumber.

then it will normal , eg: 883.04 will not be 883.0399999999

that is crazy in my view .

@icoco
Copy link

icoco commented Apr 1, 2016

anyone ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants