gpt4 book ai didi

iphone - 为什么这个 NSNumber 总和返回一个奇怪的结果?

转载 作者:行者123 更新时间:2023-11-28 20:42:35 25 4
gpt4 key购买 nike

我正在制作一个处理计算金钱的应用程序,并且已经读到它总是最好使用 NSNumber。所以我已将我的浮点值更改为 NSNumber,但现在我得到以下奇怪的结果,我不知道为什么会这样。

这是代码:

- (NSDecimalNumber*) subtotal {
NSDecimalNumber *subtotal = [NSDecimalNumber decimalNumberWithString:@"0"];
NSLog(@"subtotal initially set at %@",subtotal);
for (NSManagedObject *object in [fetchedResultsController fetchedObjects]) {
NSLog( @"Looping");
NSDecimalNumber *objectRowTotalNumber = [object valueForKey:@"total"];
NSLog(@"object row total number = %@",objectRowTotalNumber);
subtotal = [subtotal decimalNumberByAdding:objectRowTotalNumber];

NSLog(@"Subtotal: %@", subtotal);
}

return 0;
}

这是日志中的输出:

**2011-10-19 09:27:19.289 Market[4240:b603] subtotal initially set at 0**

**2011-10-19 09:27:19.290 Market[4240:b603] Looping**

**2011-10-19 09:27:19.291 Market[4240:b603] object row total number = 5**

**2011-10-19 09:27:19.292 Market[4240:b603] Subtotal: -0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000244200806570181766106054656**

当小计应该简单设置为 5 时,有人知道是什么导致了奇怪的结果吗?

最新测试:NSDecimalNumberMethod 仅适用于我在 session 中添加的新对象,但不适用于加载的对象

-(void) viewDidAppear:(BOOL)animated {
NSLog(@"I'm in ViewDidAppear");
// Reload subtotal via float method
[self subtotal];
//Reload subtotal via decimal method
[self subtotalDec];
}

用float方法计算

-(float) subtotal {
NSLog(@"I'm in subtotal using floats");

float subtotal = 0;
NSLog(@"subtotal initially set at %@",subtotal);
for (NSManagedObject *object in [fetchedResultsController fetchedObjects]) {
NSLog( @"Looping");
NSNumber *objectRowTotalNumber = [object valueForKey:@"total"];
NSLog(@"object row total number = %@",objectRowTotalNumber);
float objectRowTotal = [objectRowTotalNumber floatValue];
subtotal = subtotal + objectRowTotal;
}
NSLog(@"Subtotal: %f", subtotal);
return 0;
}

用NSDecimal Number方法计算

- (NSDecimalNumber*) subtotalDec {
NSLog(@"I'm in subtotal using NSDecimalNumber");
NSDecimalNumber *subtotal = [NSDecimalNumber decimalNumberWithString:@"0"];
NSLog(@"subtotal initially set at %@",subtotal);
for (NSManagedObject *object in [fetchedResultsController fetchedObjects]) {
NSLog( @"Looping");
NSDecimalNumber *objectRowTotalNumber = [object valueForKey:@"total"];
NSLog(@"object row total number = %@",objectRowTotalNumber);
subtotal = [subtotal decimalNumberByAdding:objectRowTotalNumber];
}
NSLog(@"Subtotal: %@",subtotal);
return 0;
}

log for view 会出现

2011-10-19 11:19:01.506 Market[961:b603] I'm in ViewDidAppear
2011-10-19 11:19:01.506 Market[961:b603] I'm in subtotal using floats
2011-10-19 11:19:01.507 Market[961:b603] subtotal initially set at (null)
2011-10-19 11:19:01.508 Market[961:b603] Looping
2011-10-19 11:19:01.508 Market[961:b603] object row total number = 4
2011-10-19 11:19:01.509 Market[961:b603] Looping
2011-10-19 11:19:01.509 Market[961:b603] object row total number = 1
2011-10-19 11:19:01.509 Market[961:b603] Looping
2011-10-19 11:19:01.510 Market[961:b603] object row total number = 10
2011-10-19 11:19:01.510 Market[961:b603] Subtotal: 15.000000
2011-10-19 11:19:01.510 Market[961:b603] I'm in subtotal using NSDecimalNumber
2011-10-19 11:19:01.511 Market[961:b603] subtotal initially set at 0
2011-10-19 11:19:01.511 Market[961:b603] Looping
2011-10-19 11:19:01.511 Market[961:b603] object row total number = 4
2011-10-19 11:19:01.512 Market[961:b603] Subtotal: -0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000244200806569055866199212032
2011-10-19 11:19:01.512 Market[961:b603] Looping
2011-10-19 11:19:01.512 Market[961:b603] object row total number = 1
2011-10-19 11:19:01.513 Market[961:b603] Subtotal: -0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000488401613129104533143683072
2011-10-19 11:19:01.513 Market[961:b603] Looping
2011-10-19 11:19:01.513 Market[961:b603] object row total number = 10
2011-10-19 11:19:01.514 Market[961:b603] Subtotal: -0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000732602419703789898877108224

addbuttontapped 的日志:注意 NSDecimalNumberMethod 如何仅对最后添加的值起作用,而不对程序启动前输入的值起作用

2011-10-20 07:17:03.218 Market[2199:b603] I'm in subtotal using floats
2011-10-20 07:17:03.218 Market[2199:b603] subtotal initially set at (null)
2011-10-20 07:17:03.219 Market[2199:b603] Looping
2011-10-20 07:17:03.219 Market[2199:b603] object row total number = 4
2011-10-20 07:17:03.219 Market[2199:b603] Looping
2011-10-20 07:17:03.219 Market[2199:b603] object row total number = 1
2011-10-20 07:17:03.220 Market[2199:b603] Looping
2011-10-20 07:17:03.220 Market[2199:b603] object row total number = 10
2011-10-20 07:17:03.220 Market[2199:b603] Looping
2011-10-20 07:17:03.221 Market[2199:b603] object row total number = 2
2011-10-20 07:17:03.221 Market[2199:b603] Subtotal: 17.000000
2011-10-20 07:17:03.221 Market[2199:b603] I'm in subtotal using NSDecimalNumber
2011-10-20 07:17:03.222 Market[2199:b603] subtotal initially set at 0
2011-10-20 07:17:03.222 Market[2199:b603] Looping
2011-10-20 07:17:03.222 Market[2199:b603] object row total number = 4
2011-10-20 07:17:03.223 Market[2199:b603] Subtotal: -0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000244200806569055866199212032
2011-10-20 07:17:03.223 Market[2199:b603] Looping
2011-10-20 07:17:03.224 Market[2199:b603] object row total number = 1
2011-10-20 07:17:03.224 Market[2199:b603] Subtotal: -0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000488401613129104533143683072
2011-10-20 07:17:03.224 Market[2199:b603] Looping
2011-10-20 07:17:03.225 Market[2199:b603] object row total number = 10
2011-10-20 07:17:03.225 Market[2199:b603] Subtotal: -0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000732602419703789898877108224
2011-10-20 07:17:03.225 Market[2199:b603] Looping
2011-10-20 07:17:03.226 Market[2199:b603] object row total number = 2
2011-10-20 07:17:03.226 Market[2199:b603] Subtotal: 2

最佳答案

我不认为 [object valueForKey:@"total"] 返回一个 NSDecimalNumber,而是一个 NSNumber

更新:

显然,这是在朝着正确的方向前进。因此,请取消删除帖子,这样我们才能正确回答问题。

我删除了这个,因为在写完之后,我尝试使用 decimalNumberByAdding: 和一个 NSNumber,但它崩溃了。但这是在 OSX 10.7 SDK 项目中,所以显然这在 iOS 中得到了不同的处理。

关于iphone - 为什么这个 NSNumber 总和返回一个奇怪的结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7814621/

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