This is a little odd to me, but when I perform basic division on a float I get inaccurate approximations. This isn't a huge开发者_如何学JAVA deal because I can truncate the number, but I'm just curious as to why NSNumbers behave this way. Sample code included...
NSString *stringNumber = @"2782";
float ratio = [stringNumber floatValue]/10000;
NSNumber *sizeRatio = [NSNumber numberWithFloat:ratio];
//core data save...
on subsequent loads the number reads: 0.2782000005245209
Any ideas? Google hasn't been very helpful because it keeps showing me results for "number" instead of "nsnumber"
This is normal for any floating point numbers
See wikipedia entry
It turns out using floats results in rather inaccurate results on iOS. I've had much better success using doubles instead.
精彩评论