添加NSDecimalNumbers不太合适

时间:2010-10-09 17:08:19

标签: iphone objective-c cocoa-touch

我有这段代码:

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似乎没有做到这一点。有什么想法吗?

谢谢!

-Max

2 个答案:

答案 0 :(得分:1)

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

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的评论中询问你的初始化代码。

此外,您的样本的以下行具有(我假设的)意外行为:

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

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

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

答案 1 :(得分:0)

我最终选择了itaiferber的建议,即将int用于所有内容,并将数据设为美分。看起来很简单。代码最终为:

- (void)totalBudgetItems {
    NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
    [numberFormatter setFormatterBehavior:NSNumberFormatterBehavior10_4];
    [numberFormatter setCurrencySymbol:@"$"];
    [numberFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];
    NSInteger currentTotal = 0;
    NSEnumerator *e = [self.defaultBudgetItemsArray objectEnumerator];
    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 + [[object objectForKey:@"actualCost"] intValue];
        }
    }
    NSNumber *tempDisplayTotalNumber = [NSNumber numberWithInt:currentTotal];
    totalActualCostLabel.text = [numberFormatter stringFromNumber:tempDisplayTotalNumber];
}

谢谢!