I'm parsing some weather JSON from a local file.
"temp_f":40.1,
"temp_c":4.5,
The problem is that when I parse it into an c value
NSLog(@"%f", [weatherDict[@"temp_f"]floatValue]);
//Logs: 40.099998
If I log it as a string value it returns 40.1
.
NSLog(@"%@", [weatherDict objectForKey:@"temp_f"]);
//Logs: 40.1
The problem seems to be when calling floatValue
Any ideas why this could be happening?
You are asking "why".
The complete answer is found in the most famous article "What every computer scientist should know about floating-point arithmetic": http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html
It is a must read unless you want to be forever guessing why strange things happen with your numbers.
BTW. Use double, not float, unless you can give me a good reason why float would be better for you.