iPhone sdk 3.2.1
NSMutableDictionary *myDict = [NSMutableDictionary dictionaryWithCapacity:2];
[myDict setObject:[NSNumber numberWithDouble:0.1f] forKey:@"testDoubleObject"];
[myDict setValue:[NSNumber numberWithDouble:0.1f] forKey:@"testDoubleValue"];
将myDict的描述打印到控制台会产生以下结果:
打印myDict的说明: {type = mutable,count = 2,capacity = 3,pairs =( 0:{contents =“testDoubleObject”} = {value = +0.10000000149011611938,type = kCFNumberFloat64Type} 1:{contents =“testDoubleValue”} = {value = +0.10000000149011611938,type = kCFNumberFloat64Type} )}
从dict中拉回双打显示正在保留这些幻像数字:
NSNumber *doubleFromObject = [myDict objectForKey:@"testDoubleObject"];
NSNumber *doubleFromValue = [myDict valueForKey:@"testDoubleValue"];
打印doubleFromObject和doubleFromValue会在控制台中生成:
打印doubleFromObject的描述: {value = +0.10000000149011611938,type = kCFNumberFloat64Type}
打印doubleFromValue的说明: {value = +0.10000000149011611938,type = kCFNumberFloat64Type}
当我尝试从字符串创建NSNumber doubleValue时会发生同样的事情:
NSString *doubleAsAString = @"0.1";
NSNumber *doubleAsANumber = [NSNumber numberWithDouble:[doubleAsAString doubleValue]];
打印doubleAsANumber在控制台中产生这个:
打印doubleAsANumber的说明: {value = +0.10000000000000000555,type = kCFNumberFloat64Type}
这些额外的幻像数字导致我出现问题。
1)导致这种情况发生的原因是什么?
2)我怎样才能摆脱那些额外的幻象数字?