What's the difference between float and double?
When I run the following code ,
NSString* s= @"10000000.01";
float f = [s floatValue];
double d = [s doubleValue];
if(f > 10000000)
{
NSLog(@"Over Value");
}
else {
NSLog(@"OK Float");
}
if(d > 10000000)
{
NSLog(@"Over value");
}
else {
NSLog(@"OK Double");
}
The response is like following.
2013-04-19 17:07:29.284 float[2991:907] OK Float
2013-04-19 17:07:29.287 float[2991:907] Over value
Why float value changed to 10000000.00 instead of 10000000.01 ?
float
is 32-bit while double
is 64-bit. A float has fewer significant digits than double.
A float
value doesn't store enough to hold the 10 digits of your 10000000.01
.
Also see Difference between float and double for more details. That is about C/C++ but it applies to Objective-C as well.
链接地址: http://www.djcxy.com/p/78624.html上一篇: 将字符串转换为在Objective C中浮动而不将其四舍五入。
下一篇: float和double有什么区别?