NSDecimalNumber 乘法奇怪

发布于 2024-08-27 11:28:29 字数 965 浏览 7 评论 0原文

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);

ExclusivePrice, quantity are both NSDecimalNumbers.

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

The result I get:

2010-04-05 00:22:29.111 TestApp[13269:207] 65 * 2 = -0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000007169919476068871316457914368

What I expected:

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

Can anyone explain this please?

Edit:

Full repro:

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);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

天涯离梦残月幽梦 2024-09-03 11:28:29

似乎对我有用。

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

Seems to work for me.

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

NSDecimalNumber * res = [n1 decimalNumberByMultiplyingBy:n2];

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

Output:

2010-04-05 08:40:42.812 x[24301] 65 * 2 = 130
桃扇骨 2024-09-03 11:28:29

我找到了答案:根据 How to use NSDecimalNumber? 我无法轻松在 NSNumber 和 NSDecimalNumber 之间转换。要从一种转换为另一种,我必须这样做:

NSDecimalNumber *n2 = [NSDecimalNumber decimalNumberWithDecimal:
                              [[formatter numberFromString:@"2"] decimalValue]];

如果 NSNumberFormatter 有一个decimalNumberFromString: 方法,那就太好了。

I found the answer: According to How to use NSDecimalNumber? I can't easily convert between NSNumber and NSDecimalNumber. To convert from the one to the other I have to do :

NSDecimalNumber *n2 = [NSDecimalNumber decimalNumberWithDecimal:
                              [[formatter numberFromString:@"2"] decimalValue]];

It would have been nice if the NSNumberFormatter had a decimalNumberFromString: method.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文