views:

54

answers:

1

Hey!

Basically I have a problem in that when certain parameters are used in my calculator app - it makes the result incorrect. The issue is that I have separate text fields for hours and minutes and say for example I have as the start time "13" in one text field and "30" in the other with the finish time "24" and "00" in their respective text fields. The answer should be 10 hours 30 minutes, but the answer I get is 11 hours 30 minutes. The code for this is the following.

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



    -(IBAction)done2:(id)sender {
  int result = [startMinutes.text intValue] - [finishMinutes.text intValue];
  totalMinutes.text = [NSString stringWithFormat:@"%d", result];
  if (result < 0) {
    totalMinutes.text = [NSString stringWithFormat:@"%d", result + 60];
  }
}

I want to make it so if certain parameters are met, then the totalHours.text is reduced by 1 hour to reflect the total minutes. How would I go about that calculation in code?

Thanks!

+2  A: 

You should first convert the start and finish times to a single unit (minutes), subtract the minutes, then convert back to hours:minutes.

For example, calc startTotalMinutes as startHours*60 + startMinutes. Do the same with the finish time. Subtract startTotalMinutes from finishTotalMinutes. Compensate for result lower than zero (I think just add 1440). Then convert that result back to hours:minutes. For the hours, do result mod 60 and the remainder is the result minutes.

Edit: Below is a code example as requested. Also note correction that the hours are result divided by 60 and the minutes are result mod 60.

- (int)hoursMinutesTextToMinutes:(NSString *)hoursText minutesText:(NSString *)minutesText
{
    return [hoursText intValue] * 60 + [minutesText intValue];
}

-(IBAction)done:(id)sender
{
    int startTotalMinutes = [self hoursMinutesTextToMinutes:startHours.text minutesText:startMinutes.text];
    int finishTotalMinutes = [self hoursMinutesTextToMinutes:finishHours.text minutesText:finishMinutes.text];

    int difference = finishTotalMinutes - startTotalMinutes;
    if (difference < 0)
    {
        difference += 1440;
    }

    int diffHours = difference / 60;
    int diffMinutes = difference % 60;

    totalHours.text = [NSString stringWithFormat:@"%d", diffHours];
    totalMinutes.text = [NSString stringWithFormat:@"%d", diffMinutes];
}

The hoursMinutesTextToMinutes is a helper method that returns hours:minutes text as just a minutes int. This avoids duplicating that code.

Only one button action is needed (I used "done").

Please note this does not do any validation on the text entered. You should add validation or use a UIDatePicker to let user specify the start/finish times.

DyingCactus
Another thing is you don't need two action methods. You would just need one button that when clicked does the above logic.
DyingCactus
How would I go about that in code? Maths wasn't my strongest area of expertise. :/
LawVS
I've added a code example.
DyingCactus