简体   繁体   English

如何将浮点值四舍五入到小数点后两位?

[英]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 -3.04299553323

I'd like to get -3.04 for example. 例如,我想得到-3.04。 Is there an easy way for rounding that float value? 是否有简单的方法可以舍入该浮点值?

Edit: 编辑:

Rounding numbers in Objective-C 在Objective-C中四舍五入数字

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 结果= -3.04

The @"%.2f" will round to two decimal places. @“%。2f”将舍入到小数点后两位。 If you want three decimal places put @"%.3f" and so on. 如果要保留小数点后三位,请输入@“%。3f” ,依此类推。

Hope this helps! 希望这可以帮助!

You should only ever do this while formatting a number for display to the end user, because there is no guarantee that 您仅应在格式化数字以显示给最终用户时执行此操作,因为无法保证

float rounded = roundf(orig * 100) / 100.0;

or similar will return an exact answer. 或类似内容将返回确切答案。

Indeed, in general, it won't. 实际上,总的来说不会。 For instance consider 例如考虑

float f = 123456.3;
float r = roundf(f * 100) / 100.0;

printf("%.2f, %.10f\n", r, r);

which outputs 哪个输出

123456.30, 123456.2968750000

Oops! 糟糕!

Using double rather than float helps, but even so, if we change the code a little, you can see that it doesn't really solve the problem: 使用double而不是float有所帮助,但是即使如此,如果我们稍微更改代码,您也会看到它并不能真正解决问题:

double f = 123456.3;
double r = round(f * 100) / 100.0;

printf("%.2f, %.20f\n", r, r);

Again, we get 再次,我们得到

123456.30, 123456.30000000000291038305

which shows quite clearly that it isn't exactly rounded. 很清楚地表明它不是完全四舍五入的。

Anyway, the moral of the story is that doing things like round(f * 100) / 100.0 only rounds approximately . 无论如何,这个故事的寓意是,做round(f * 100) / 100.0类的事情大约只会四舍五入。 It might be good enough in some cases, but you do need to keep in mind that the result is not really rounded. 在某些情况下,这可能已经足够好了,但是您需要记住,结果并不是真正的四舍五入。

If you want something better, you'll need to use decimal arithmetic instead. 如果您想要更好的东西,则需要使用十进制算法。 You can either do this by keeping your values as integers (eg for currency values, keep them in pence or cents instead of pounds or dollars), or by using one of the various decimal floating point packages you can find on Wikipedia's Decimal Floating Point page . 您可以通过将值保留为整数来实现此目的(例如,对于货币值,将其保留为便士或美分而不是英镑或美元),或者使用各种小数点浮点程序包之一,您可以在Wikipedia的小数点浮点页面上找到

我只是发布对这个问题的答案,因为这是我快速获得此效果的唯一方法,因为我合并了两个给定的答案以在iPad应用程序中向用户显示四舍五入的float值:

NSString *roundedAmount = [NSString stringWithFormat:@"%.2f", round ((floatValue / 1024) * 100.0) / 100.0];

Multiply it by 100, (round up/down to nearest integer if necessary), take the integer portion, then divide by 100 again 乘以100(如果需要,向上/向下舍入到最接近的整数),取整数部分,然后再除以100

Applies for any number decimals places, multiply/divide by 10^(no. of decimals). 适用于任何数字小数位数,乘以10除以10 ^(小数位数)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM