Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
191 views
in Technique[技术] by (71.8m points)

iphone - Adding NSDecimalNumbers not quite working

I have this code:

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

The console output is:

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)

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

Thanks!

-Max

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...