DataGuy
DataGuy

Reputation: 1725

Floating number issue

All,

I have a floating number issue that I need help with. I just started with Objective-C and this seems to be a pretty simple solution, but I'm at a loss after my search. In short, I'm adding the numerical input from four TextFields together and displaying the answer in a UILabel. My answer is in the correct decimal format, but only displays 0.00 and not the correct sum. Im sure it it something to do with converting between string and float, but I need some assistance:

- (IBAction)Calc1:(id)sender {

    int result = [oranges.text doubleValue] + 
                    [bananas.text doubleValue] +
                        [grapes.text doubleValue] +
                            [pears.text doubleValue];

    text123.text = [NSString stringWithFormat:@"%.1f", result];

}

Upvotes: 0

Views: 109

Answers (2)

xda1001
xda1001

Reputation: 2459

You use int type to store double type, then accuracy is lost.

You should set the result variable to the double type.

Upvotes: 0

Paul Hunter
Paul Hunter

Reputation: 4463

You probably want to declare result as a double...

double result = ....

Upvotes: 3

Related Questions