I need to be able to calculate the time difference between two dates and then store that value in one digit precision decimal format. So if the time difference is 6 minutes then I need .1 to be returned, if it is 3 minutes I still need .1 returned, but if it is 7 minutes or 12 minutes, I need .2 to be returned. The subtraction of the time difference is working correctly:
NSTimeInterval interval = [currentFlight.landtime timeIntervalSinceDate:currentFlight.takeofftime];
That returns as expected so if the difference is 6 (minutes), the value I get back is 0.100000.
I also need to display this value to the user in a UILabel. When I use this number formatter, 0.100000 gets displayed as .2 instead of .1:
NSNumberFormatter *ftDecimalFormatter = [[NSNumberFormatter alloc] init];
[ftDecimalFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
[ftDecimalFormatter setMaximumFractionDigits:1];
[ftDecimalFormatter setMinimumFractionDigits:1];
[ftDecimalFormatter setRoundingMode:NSNumberFormatterRoundUp];
I have tried all the difference rounding methods, but none seem to work. Any suggestions?
Multiply the number by 10, add 0.9999999999999, truncate the fractional part (ie floor), then divide by 10. [You can achieve the same result other ways this is just one way.] Then you can just print your number with a format string specifying one decimal digit, no?