Note that there are some explanatory texts on larger screens.

plurals
  1. POTimeInterval calculation issue
    text
    copied!<p>I am trying to create an app that calculates the time difference and the amount is multiplied by an amount in money. It is targeted to calculate in R$ (brazilian real) the amount that someone will have to pay for using a service during the time calculated by the app.</p> <p>Here´s is my code:</p> <pre><code>- (IBAction)encerrar:(id)sender { NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; dateFormatter.dateFormat = @"HH:mm"; NSString *temp = inicio.text; NSDate *then = [dateFormatter dateFromString:temp]; NSDate *now = [NSDate date]; NSTimeInterval timeInterval = [now timeIntervalSinceDate:then]; NSString *ext = [dateFormatter stringFromDate:now]; fim.text = ext; NSDate *timerDate = [NSDate dateWithTimeIntervalSince1970:timeInterval]; [dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0.0]]; NSString *timeString = [dateFormatter stringFromDate:timerDate]; duracao.text = timeString; double timeIntervalInHours = (timeInterval / 3600.0) * 5; NSString *vTotal = [[NSString alloc] initWithFormat:@"R$ %.2f", fecha]; NSLog(@"%.2f", timeIntervalInHours); vlrTotal.text = vTotal; } </code></pre> <p>The fact is that when we click on Calculate button, and if the duracao (duration) is equal to 1h it gives me the correct amount which is R$ 5,00. But when the duration is equal to 30mins or other value different from exact 1h, it gives the wrong amount.</p> <p>I.E.: 1h should be R$ 5,00; while 1:30h should be R$ 7,50, but shows me R$ 6,50.</p> <p>So, anyone could help me on this???</p> <p>Thanks in advance!!!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload