Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy is this NSNumber sum throwing back an odd result?
    primarykey
    data
    text
    <p>I am making an app that deals with calculating money and have read that its always best to use NSNumber for this. So I have changed my float values to NSNumber but now I get the following odd result and I don't know why it is occurring.</p> <p>This is the code:</p> <pre><code>- (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; } </code></pre> <p>This is the output in the log:</p> <pre><code>**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** </code></pre> <p>Would anyone know what is causing the odd result when the subtotal should simple be set to 5?</p> <p><strong>Latest Test</strong> : The NSDecimalNumberMethod works only on new objects that I add in the session, but not on objects that are loaded</p> <pre><code>-(void) viewDidAppear:(BOOL)animated { NSLog(@"I'm in ViewDidAppear"); // Reload subtotal via float method [self subtotal]; //Reload subtotal via decimal method [self subtotalDec]; } </code></pre> <p>Calculating with float method</p> <pre><code>-(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; } </code></pre> <p>Calculating with NSDecimal Number Method</p> <pre><code>- (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; } </code></pre> <p>log for view Will appear</p> <pre><code>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 </code></pre> <p>log for addbuttontapped: Notice how the NSDecimalNumberMethod works only on the last added value, not on the ones that were entered before the program was started</p> <pre><code>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 </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload