views:

54

answers:

2

what's the most elegant way of rounding a float to it's closest 1/4ths in Objective C?

For example:

3.14 should be 3.25
2.72 should be 2.75
6.62 should be 6.50
1.99 should be 2.00

etc.

+5  A: 
#include <math.h>
...

newValue = round (value * 4.0) / 4.0;
KennyTM
+3  A: 

One approach is to use an NSNumberFormatter with a rounding increment and rounding mode set. You can then access the float value. Obviously, this assumes Cocoa and is more than a little heavy-handed.

Justin