views:

34

answers:

2

Hey guys,

Basically I've been trying to make a working hours calculator and I've run into a problem. When the start time's value is greater than the finish time (eg. start is 23 and finish is 19), the result comes up as a negative. So what I want it to do in that scenario is to then multiply the negative number by -1 to make it positive again. However, this code below doesn't seem to have any effect in my app.

-(IBAction)done:(id)sender {
    int result = [finishHours.text intValue] - [startHours.text intValue];
    totalHours.text = [NSString stringWithFormat:@"%i", result];
    if (result < 0) {
            result = result * -1;

    }

Any help would be greatly would be greatly appreciated. :)

+4  A: 

You set totalHours.text before you change the sign of your result variable.

Vladimir
Thank you, Vladmir. You've helped me tons!
LawVS
+1  A: 

what do you mean it doesn't have any effect? is it because you have changed result after you have set totalHours? would this fix your issue?

-(IBAction)done:(id)sender {
int result = [finishHours.text intValue] - [startHours.text intValue];
if (result < 0) {
        result = result * -1;

}
totalHours.text = [NSString stringWithFormat:@"%i", result];
Sam Holder