ios - JSON string becomes different value when converting to float -


i'm parsing weather json local file.

 "temp_f":40.1,  "temp_c":4.5, 

the problem when parse c value

nslog(@"%f", [weatherdict[@"temp_f"]floatvalue]);  //logs: 40.099998 

if log string value returns 40.1.

nslog(@"%@", [weatherdict objectforkey:@"temp_f"]); //logs: 40.1 

the problem seems when calling floatvalue

any ideas why happening?

you asking "why".

the complete answer found in famous article "what every computer scientist should know floating-point arithmetic": http://docs.oracle.com/cd/e19957-01/806-3568/ncg_goldberg.html

it must read unless want forever guessing why strange things happen numbers.

btw. use double, not float, unless can give me reason why float better you.


Comments

Popular posts from this blog

javascript - AngularJS custom datepicker directive -

javascript - jQuery date picker - Disable dates after the selection from the first date picker -