Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I round a float value to 2 post decimal positions?

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:

Rounding numbers in Objective-C

like image 550
Thanks Avatar asked May 06 '09 10:05

Thanks


1 Answers

I know this is old post but just in case someone else is looking for a quick Two step option.

float old = -3.04299553323;  
float new = [[NSString stringWithFormat:@"%.2f",old]floatValue];

Result = -3.04

The @"%.2f" will round to two decimal places. If you want three decimal places put @"%.3f" and so on.

Hope this helps!

like image 53
15 revs Avatar answered Sep 23 '22 19:09

15 revs