问题
I have a double in an NSNumber.
double myDouble = 1363395572.6129999;
NSNumber *doubleNumber = @(myDouble);
// using [NSNumber numberWithDouble:myDouble] leads to the same result
This is where it gets problematic.
doubleNumber.doubleValue seems to return the correct and full value (1363395572.6129999)
However, looking at doubleNumber in the debugger or doing doubleNumber.description gives me (1363395572.613).
I would understand if perhaps this was just some display formatting, but when I then stick this object into a JSON payload, the messed up rounded value gets inserted instead of the actual number.
The way I'm doing this is something like this:
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:(Dictionary containing NSNumber)
options:0 error:nil];
NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
Looking at the string at this point shows me the truncated number with 3 decimal places even though the NSNumber I inserted had 7.
My question is why is this happening and more importantly how can I stop it from happening?
EDIT with conclusion:
For anyone who stumbles onto this, the problem was not clear to me from the beginning but the actual issue is that NSNumber and double are both incapable of holding a number with the sort of precision I am looking for. As Martin's answer shows, my problem occurred as soon as I deserialized the initial number values from a JSON response.
I ended up working around my problem by reworking the whole system to stop depending on this level of precision(since these are timestamps, microseconds) of these numbers on the client, and instead use a different identifier to pass around with the API.
As Martin and Leo pointed out, in order to get around this problem one would need to use a custom JSON parser that allows parsing of a JSON number into an NSDecimalNumber rather than an NSNumber. A better solution to my problem in particular was what I outlined in the previous paragraph, so I did not pursue this route.
来源:https://stackoverflow.com/questions/23019338/nsnumber-double-with-many-decimal-places-being-rounded-truncated