gpt4 book ai didi

objective-c - NSDecimalNumber 乘法奇怪

转载 作者:太空狗 更新时间:2023-10-30 03:59:15 27 4
gpt4 key购买 nike

ExclusivePrice,数量都是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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com