NSNumber stringValue different from NSNumber value

安稳与你 提交于 2019-12-03 09:02:59

You are discovering that floating point numbers can't always be represented exactly. There are numerous posts about such issues.

If you need to get back to the original string, then keep the original string as your data and only convert to a number when you need to perform a calculation.

You may want to look into NSDecimalNumber. This may better fit your needs.

NSString *numStr = @"9.2";
NSDecimalNumber *decNum = [NSDecimalNumber decimalNumberWithString:numStr];
NSString *newStr = [decNum stringValue];
NSLog(@"decNum = %@, newStr = %@", decNum, newStr);

This gives 9.2 for both values.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!