开发者

NSNumbers are inaccurate when retrieved using core data

开发者 https://www.devze.com 2023-03-31 16:51 出处:网络
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 curi

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号