views:

940

answers:

2

I've got a float value from an accelerometer which looks like this:

-3.04299553323

I'd like to get -3.04 for example. Is there an easy way for rounding that float value?

Edit:

http://stackoverflow.com/questions/752817/rounding-numbers-in-objective-c

+2  A: 

Multiply it by 100, (round up/down to nearest integer if necessary), take the integer portion, then divide by 100 again

Applies for any number decimals places, multiply/divide by 10^(no. of decimals).

+2  A: 
float f = -3.04299553323
int i = f *100; //-304
float answer = i/100 ;//-3.04
Visage
This truncates to 2 digits instead of rounding.
Ralph Rickenbach
instead use int i = f * 100 + 0.5;
Ralph Rickenbach
Yes, good point.
Visage