Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

larswik

macrumors 68000
Original poster
Sep 8, 2006
1,552
11
I seem to have all these odd problems that I am struggling to find the answer for, this one seems so basic. I have a Table view that loads when the user clicks a button. I then iterate through the dollar amounts stoered in the mutableArray and add up the amounts for a total, but the math is off?

I think it has something to do with the dequeueReusableCellWithIdentifier since the final 2 that are supposed to be added to the total are skipped. I had a problem many months ago when values when off the screen they returned as 0, but I solved that. But in this instance I an just dealing with the array and the values in it's indexes?

Code:
-(void)addUpTheValues{ // calculates the values in the text fields
    runningGrandTotal = 0.0;
    
    for (int i = 0; i < totalPayments.count; i++) {
        NSString *TempTwoString = [totalPayments objectAtIndex:i];
        runningGrandTotal +=   [TempTwoString floatValue];
        
        NSLog(@"Temp %d : %@", i, TempTwoString);
        NSLog(@"The Running Grand Total is %.2f", runningGrandTotal);
    }
    grandTotal.text = [NSString stringWithFormat:@"%.2f",runningGrandTotal];
   
}

Here is the print out from the NSLog. It adds everything up and then stops at 2300.00 at "Temp 8" and on? Thje values are there but it is not adding them in?
2011-11-23 13:50:38.858 Money List[4679:607] Temp 0 : 400.00
2011-11-23 13:50:38.860 Money List[4679:607] The Running Grand Total is 400.00
2011-11-23 13:50:38.863 Money List[4679:607] Temp 1 : 200.00
2011-11-23 13:50:38.865 Money List[4679:607] The Running Grand Total is 600.00
2011-11-23 13:50:38.867 Money List[4679:607] Temp 2 : 800.00
2011-11-23 13:50:38.869 Money List[4679:607] The Running Grand Total is 1400.00
2011-11-23 13:50:38.870 Money List[4679:607] Temp 3 : 100.00
2011-11-23 13:50:38.872 Money List[4679:607] The Running Grand Total is 1500.00
2011-11-23 13:50:38.873 Money List[4679:607] Temp 4 : 200.00
2011-11-23 13:50:38.882 Money List[4679:607] The Running Grand Total is 1700.00
2011-11-23 13:50:38.884 Money List[4679:607] Temp 5 : 200.00
2011-11-23 13:50:38.885 Money List[4679:607] The Running Grand Total is 1900.00
2011-11-23 13:50:38.887 Money List[4679:607] Temp 6 : 200.00
2011-11-23 13:50:38.889 Money List[4679:607] The Running Grand Total is 2100.00
2011-11-23 13:50:38.890 Money List[4679:607] Temp 7 : 200.00
2011-11-23 13:50:38.896 Money List[4679:607] The Running Grand Total is 2300.00
2011-11-23 13:50:38.899 Money List[4679:607] Temp 8 : $400.00
2011-11-23 13:50:38.903 Money List[4679:607] The Running Grand Total is 2300.00
2011-11-23 13:50:38.906 Money List[4679:607] Temp 9 : $750.00
2011-11-23 13:50:38.909 Money List[4679:607] The Running Grand Total is 2300.00
 
You put a $ character before the last two entries: Temp 8 and Temp 9. The float value in those cases is 0, which is why the total isn't increasing. Remove the $ character from the last two entries and things should work.
 
Using a string to represent a number used in a calculation is a bad idea. Keep the number. Calculate with the number. To display a number, convert it to a string. The primary representation is always the number.
 
OMG! I was looking at that for about 3 hours last night and the answer was right in front of me. Now I feel dumb and thanks for the second set of eyes that caught that. I guess that was a good thing while I am flushing out my code. I should build in make some method that will check to see that only valid characters are in a textField to avoid that.

Chown33- my numbers are stored in an NSMutableArray and from what I understand I can not store primitive data types in that type of array. These arrays are also stored in a Property List, which I also think need to be objects and not primitive data types. I must first wrap them in an object before I can store them. I have gotten use to using NSString as my go to object for this. I don't see a big difference between something like NSString and NSNumber to wrap my int's or floats in?

I understand what you are saying about "Calculate with numbers". so instead of this line of code

Code:
runningGrandTotal +=   [TempTwoString floatValue];

It should be something like

Code:
float someNumber =   [TempTwoString floatValue];
runningGrandTotal += someNumber;

Thanks again for the assist!
 
The conversion of an int or float to and from an NSString is very expensive. The conversion of an int or double to and from an NSNumber is almost trivial.
 
Hummm.... I will adopt that and read up on the NSNumber class. I am curious why it is more expensive in terms of compute time with NSString then NSNumber? I will read up on it tonight and thanks for pointing that out Jim
 
I did a quick'n'dirty benchmark.


With ints:

Conversion of 10,000,000 ints to and from NSNumbers took 0 seconds
Conversion of 10,000,000 ints to and from NSStrings took 11 seconds

Conversion of 100,000,000 ints to and from NSNumbers took 4 seconds
Conversion of 100,000,000 ints to and from NSStrings took 113 seconds


And with doubles:

Conversion of 10,000,000 doubles to and from NSNumbers took 2 seconds
Conversion of 10,000,000 doubles to and from NSStrings took 14 seconds

Conversion of 100,000,000 doubles to and from NSNumbers took 17 seconds
Conversion of 100,000,000 doubles to and from NSStrings took 146 seconds
 
Last edited:
Interesting finds... I wasn't aware converting to one type of NSObject would be any better than converting to any other, for performance reasons.
 
Wow, some benchmarks. I guess when you are designing complex things and games it is good to use the correct object for the task at hand.

Chown33 - I looked at the doc's and thought that you could only have integers in an NSNumbers but it said this
accessing the value as a signed or unsigned char, short int, int, long int, long long int, float, or double

Char is a character and not an integer value? So I guess NSNumber can hold more the numbers.
 
Wow, some benchmarks. I guess when you are designing complex things and games it is good to use the correct object for the task at hand.

Chown33 - I looked at the doc's and thought that you could only have integers in an NSNumbers but it said this


Char is a character and not an integer value? So I guess NSNumber can hold more the numbers.

Char is a number between 0 and 127 I believe.
 
Wow, some benchmarks. I guess when you are designing complex things and games it is good to use the correct object for the task at hand.

It's always goot to use the correct object for the task at hand. Granted though that our CPUs and RAM are big enough to cope with sloppy programming.


Char is a number between 0 and 127 I believe.

-128 to 127 or 0 to 255 depending on if it's signed or not.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.