I wrote a sample to convert string to float in Objective-C:
NSString *sampleFloatString = @"1.3";
float sampleFloatValue = [sampleFloatString floatValue];
But when I display sampleFloatValue
, it shows '1.29999995'. I know it's equal to 1.3, but why is it not exactly '1.3'? Why do we need to format it explicitly? Is there any other way of doing this conversion?
Its called "Floating point error". The way that computers represent decimal numbers causes them to not be 100% accurate all the time:
http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html