ExclusivePrice、quantity 都是 NSDecimalNumbers。

NSDecimalNumber *price = [exclusivePrice decimalNumberByMultiplyingBy:quantity];
NSLog(@"%@ * %@ = %@", exclusivePrice, quantity, price);

我得到的结果:
2010-04-05 00:22:29.111 TestApp[13269:207] 65 * 2 = -0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000007169919476068871316457914368

我的期望:
2010-04-05 00:22:29.111 TestApp[13269:207] 65 * 2 = 130

有人可以解释一下吗?

编辑:

完整再现:
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setNumberStyle:NSNumberFormatterDecimalStyle];
NSDecimalNumber *n2 = (NSDecimalNumber *)[formatter numberFromString:@"2"];
NSDecimalNumber *n1 = (NSDecimalNumber *)[NSDecimalNumber numberWithInt:65];
NSDecimalNumber *n3 = [n1 decimalNumberByMultiplyingBy:n2];
NSLog(@"%@ * %@ = %@", n1, n2, n3);

最佳答案

似乎对我有用。

NSDecimalNumber * n1 = [NSDecimalNumber decimalNumberWithString:@"65"];
NSDecimalNumber * n2 = [NSDecimalNumber decimalNumberWithString:@"2"];

NSDecimalNumber * res = [n1 decimalNumberByMultiplyingBy:n2];

NSLog(@"%@ * %@ = %@", n1, n2, res);

输出:
2010-04-05 08:40:42.812 x[24301] 65 * 2 = 130

关于objective-c - NSDecimalNumber 乘法奇怪,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/2576109/

10-09 16:24