Tremaine
Tremaine

Reputation: 53

Add floatValues to NSArray add then output total

My brain is fried! I can't think.

I'm using Core Data, and this is an iPhone app just so you know. What i am trying to do is take the floatValues (like amount paid) from text fields and them to an array, add the values in the array and output the total to a label.

For some odd reason I honestly can't think.I'm scattered brained at the moment. Please help!

Thanks in advance. -T.

Upvotes: 0

Views: 464

Answers (2)

Deepak Danduprolu
Deepak Danduprolu

Reputation: 44633

You can add it to the array using NSNumber or its subclass NSDecimalNumber. NSDecimalNumbers can be added using decimalNumberByAdding: too.

Upvotes: 0

grundprinzip
grundprinzip

Reputation: 2491

The NSArray in Objective C stores only Objects, so you will need to convert the float integral value into an NSNumber object using the following code and of course back.

[arr addObject: [NSNumber numberWithFloat: fV]];

float total = 0.0;
for (NSNumber v in arr)
{
    total += [v floatValue];
}

However, you could easily create a float[] c-style array and iterate with a simple for loop over it.

Upvotes: 1

Related Questions