Search code examples
ios6rounding-errorrounding

Rounding up a number not always works as expected


I want to charge my users 1 credit for each hour or fraction they use a service. To calculate the cost I'm using the following code, but in some cases, for example when the starting and ending dates are exactly at one day difference, I get a cost of 25 credits instead of 24:

NSNumberFormatter *format = [[NSNumberFormatter alloc]init];
[format setNumberStyle:NSNumberFormatterDecimalStyle];
[format setRoundingMode:NSNumberFormatterRoundUp];
[format setMaximumFractionDigits:0];
[format setMinimumFractionDigits:0];
NSTimeInterval ti = [endDate timeIntervalSinceDate:startDate];
float costValue = ti/3600;
self.cost = [format stringFromNumber:[NSNumber numberWithFloat:costValue]];

What am I doing wrong?


Solution

  • NSTimeInterval has sub-millisecond precision. If the dates are one day and a millisecond apart, you would charge the 25-th credit.

    Changing the code to do integer division should deal with the problem:

    // You do not need sub-second resolution here, because you divide by 
    // the number of seconds in the hour anyway
    NSInteger ti = [endDate timeIntervalSinceDate:startDate];
    NSInteger costValue = (ti+3599)/3600;
    // At this point, the cost is ready. You do not need a special formatter for it.