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

前端 未结 4 1256
我在风中等你
我在风中等你 2020-11-29 09:23

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 tha

4条回答
  •  没有蜡笔的小新
    2020-11-29 10:15

    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!

提交回复
热议问题