本文介绍了添加NSDecimalNumbers不太起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这段代码:

NSLog(@"Count of items we will loop through is: %d",[self.defaultBudgetItemsArray count]);
id object;
while (object = [e nextObject]) {
    if ([object objectForKey:@"actualCost"]) {
        currentTotal = [currentTotal decimalNumberByAdding: [object objectForKey:@"actualCost"]];
        NSLog(@"decimalNumberByAdding gives: %@",[numberFormatter stringFromNumber:[currentTotal decimalNumberByAdding: [object objectForKey:@"actualCost"]]]);
        NSLog(@"Trying to add: %@",[numberFormatter stringFromNumber:[object objectForKey:@"actualCost"]]);
    }
}

totalActualCostLabel.text = [numberFormatter stringFromNumber:currentTotal];
NSLog(@"Budget items total: %@",[numberFormatter stringFromNumber:currentTotal]);

控制台输出为:

2010-10-09 12:58:45.285 App[11659:307] Count of items we will loop through is: 6
2010-10-09 12:58:45.287 App[11659:307] decimalNumberByAdding gives: ($0.00)
2010-10-09 12:58:45.289 App[11659:307] Trying to add: $0.00
2010-10-09 12:58:45.292 App[11659:307] decimalNumberByAdding gives: ($0.00)
2010-10-09 12:58:45.293 App[11659:307] Trying to add: $0.00
2010-10-09 12:58:45.296 App[11659:307] decimalNumberByAdding gives: ($0.00)
2010-10-09 12:58:45.301 App[11659:307] Trying to add: $0.00
2010-10-09 12:58:45.303 App[11659:307] decimalNumberByAdding gives: ($0.00)
2010-10-09 12:58:45.305 App[11659:307] Trying to add: $5.00
2010-10-09 12:58:45.307 App[11659:307] decimalNumberByAdding gives: ($0.00)
2010-10-09 12:58:45.309 App[11659:307] Trying to add: $0.00
2010-10-09 12:58:45.311 App[11659:307] decimalNumberByAdding gives: ($0.00)
2010-10-09 12:58:45.318 App[11659:307] Trying to add: $0.00
2010-10-09 12:58:45.320 App[11659:307] Budget items total: ($0.00)

请注意,尝试添加行之一表示$ 5.00,但decimalnumberbyadding似乎并未执行该操作。有想法吗?

Notice that one of the "Trying to add" lines says $5.00 but decimalnumberbyadding doesn't seem to be doing its thing. Any ideas?

谢谢!

-Max

推荐答案

我们没有足够的信息来帮助您解决特定问题。也就是说,我尝试了以下代码来初始化您的代码:

We don't have quite enough information to help you solve your specific issue. That said, I tried the following code to initialise yours:

NSArray *ary = [NSArray arrayWithObjects:
    [NSDictionary dictionaryWithObject:[NSDecimalNumber decimalNumberWithString:@"0"] forKey:@"actualCost"], 
    [NSDictionary dictionaryWithObject:[NSDecimalNumber decimalNumberWithString:@"4"] forKey:@"actualCost"], 
    [NSDictionary dictionaryWithObject:[NSDecimalNumber decimalNumberWithString:@"5"] forKey:@"actualCost"], 
    [NSDictionary dictionaryWithObject:[NSDecimalNumber decimalNumberWithString:@"2"] forKey:@"actualCost"], nil];

NSEnumerator *e = [ary objectEnumerator];
NSDecimalNumber *currentTotal = [NSDecimalNumber zero];
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
[numberFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];

上面的代码似乎没有任何戏剧性,所以我不得不第二个@NSResponder的注释询问

The above code seems to work with no dramas, so I'll have to second @NSResponder's comment asking for your initialisation code.

此外,示例的以下行(我认为是)具有意外行为:

Also, the following line of your sample has (what I assume to be) unintended behaviour:

NSLog(@"decimalNumberByAdding gives: %@",[numberFormatter stringFromNumber:[currentTotal decimalNumberByAdding: [object objectForKey:@"actualCost"]]]);

这不会导致总数不正确,但是日志输出会产生误导。机器到达该行时,它将已经添加了当前总和,并且您的日志将使其看起来已被添加了两次。我认为您可能想要以下内容:

It does not cause your total to be incorrect, but the log output will be misleading. By the time the machine has reached that line, it will have already added the current sum and your log will make it appear to have been added twice. I think you probably want the following:

NSLog(@"decimalNumberByAdding gives: %@", [numberFormatter stringFromNumber:currentTotal]);

这篇关于添加NSDecimalNumbers不太起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 08:51