views:

1577

answers:

3

NSTimeInterval == double; (e.g. 169.12345666663)

How can I round up this double so that there are only 2 digits left after the "dot"?
It would be very good if the result is a NSNumber.

A: 

Does this HumanReadableTimeInterval help? It returns a NSString, though. Alternatively, you can round yourself by multiplying with 100, converting to an integer and dividing through 100 again.

schnaader
+1  A: 

In the vast majority of cases rounding a number is something you should only do at display time. The properties of floating-point numbers (double or not) make it impossible to store certain numbers at a fixed-precision.

For information about formatting a number so it displays to two decimal places, see this other post.

Andrew Grant
+3  A: 

If this is for display purposes, take a look at NSNumberFormatter.

If you really want to round the double in your calculations for some reason, you can use the standard C round() function.

Chuck