你走在正确的轨道上(有一些警告,见下文)。您应该能够只使用来自 NSNumber 的初始化程序,因为它们是由 NSDecimalNumber 继承的。
NSDecimalNumber *floatDecimal = [[[NSDecimalNumber alloc] initWithFloat:42.13f] autorelease];
NSDecimalNumber *doubleDecimal = [[[NSDecimalNumber alloc] initWithDouble:53.1234] autorelease];
NSDecimalNumber *intDecimal = [[[NSDecimalNumber alloc] initWithInt:53] autorelease];
NSLog(@"floatDecimal floatValue=%6.3f", [floatDecimal floatValue]);
NSLog(@"doubleDecimal doubleValue=%6.3f", [doubleDecimal doubleValue]);
NSLog(@"intDecimal intValue=%d", [intDecimal intValue]);
关于这个主题的更多信息可以找到here。
但是,我在 Stack Overflow 和网络上看到了很多关于初始化 NSDecimalNumber 问题的讨论。似乎有一些与精度和与NSDecimalNumber 之间的双精度转换有关的问题。尤其是当你使用继承自 NSNumber 的初始化成员时。
我敲了下面的测试:
double dbl = 36.76662445068359375000;
id xx1 = [NSDecimalNumber numberWithDouble: dbl]; // Don't do this
id xx2 = [[[NSDecimalNumber alloc] initWithDouble: dbl] autorelease];
id xx3 = [NSDecimalNumber decimalNumberWithString:@"36.76662445068359375000"];
id xx4 = [NSDecimalNumber decimalNumberWithMantissa:3676662445068359375L exponent:-17 isNegative:NO];
NSLog(@"raw doubleValue: %.20f, %.17f", dbl, dbl);
NSLog(@"xx1 doubleValue: %.20f, description: %@", [xx1 doubleValue], xx1);
NSLog(@"xx2 doubleValue: %.20f, description: %@", [xx2 doubleValue], xx2);
NSLog(@"xx3 doubleValue: %.20f, description: %@", [xx3 doubleValue], xx3);
NSLog(@"xx4 doubleValue: %.20f, description: %@", [xx4 doubleValue], xx4);
输出是:
raw doubleValue: 36.76662445068359375000 36.76662445068359375
xx1 doubleValue: 36.76662445068357953915, description: 36.76662445068359168
xx2 doubleValue: 36.76662445068357953915, description: 36.76662445068359168
xx3 doubleValue: 36.76662445068357953915, description: 36.76662445068359375
xx4 doubleValue: 36.76662445068357953915, description: 36.76662445068359375
所以你可以看到,当在NSNumber 上使用numberWithDouble 便捷方法时(由于它返回错误的指针类型,你不应该真正使用它)甚至初始化器initWithDouble (IMO“应该”可以调用)你得到一个带有内部表示的 NSDecimalNumber(通过在对象上调用 description 返回),它不如你调用 decimalNumberWithString: 或 decimalNumberWithMantissa:exponent:isNegative: 时得到的准确。
另外,请注意,通过在 NSDecimalNumber 实例上调用 doubleValue 转换回双精度会丢失精度,但有趣的是,无论您调用什么初始化程序,都是一样的。
所以最后,我认为在处理高精度浮点数时,我认为建议您使用在 NSDecimalNumber 类级别声明的 decimalNumberWith* 方法之一来创建您的 NSDecimalNumber 实例。
那么当你有一个 double、float 或其他 NSNumber 时,你如何轻松地调用这些初始化器呢?
两种方法描述了here“工作”,但仍然存在精度问题。
如果您已经将号码存储为 NSNumber,那么这个应该可以工作:
id n = [NSNumber numberWithDouble:dbl];
id dn1 = [NSDecimalNumber decimalNumberWithDecimal:[n decimalValue]];
NSLog(@" n doubleValue: %.20f, description: %@", [n doubleValue], n);
NSLog(@"dn1 doubleValue: %.20f, description: %@", [dn1 doubleValue], dn1);
但正如您从下面的输出中看到的那样,它去掉了一些不太重要的数字:
n doubleValue: 36.76662445068359375000, description: 36.76662445068359
dn1 doubleValue: 36.76662445068357953915, description: 36.76662445068359
如果数字是原始数字(浮点数或双精度数),那么这个应该工作:
id dn2 = [NSDecimalNumber decimalNumberWithMantissa:(dbl * pow(10, 17))
exponent:-17
isNegative:(dbl < 0 ? YES : NO)];
NSLog(@"dn2 doubleValue: %.20f, description: %@", [dn2 doubleValue], dn2);
但是您会再次遇到精度错误。正如您在下面的输出中看到的:
dn2 doubleValue: 36.76662445068357953915, description: 36.76662445068359168
我认为这种精度损失的原因是由于涉及浮点乘法,因为以下代码可以正常工作:
id dn3 = [NSDecimalNumber decimalNumberWithMantissa:3676662445068359375L
exponent:-17
isNegative:NO];
NSLog(@"dn3 doubleValue: %.20f, description: %@", [dn3 doubleValue], dn3);
输出:
dn3 doubleValue: 36.76662445068357953915, description: 36.76662445068359375
因此,从 double 或 float 到 NSDecimalNumber 的最一致准确的转换/初始化是使用 decimalNumberWithString:。但是,正如 Brad Larson 在他的回答中指出的那样,这可能有点慢。如果性能成为问题,他使用 NSScanner 进行转换的技术可能会更好。