如何将浮点值舍入到小数点后2位?

时间:2021-09-29 17:07:36

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. Is there an easy way for rounding that float value?

我想以-3.04为例。是否有简单的方法来舍入浮动值?

Edit:

编辑:

Rounding numbers in Objective-C

Objective-C中的舍入数字

4 个解决方案

#1


39  

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. If you want three decimal places put @"%.3f" and so on.

@“%。2f”将舍入到小数点后两位。如果你想要三个小数位放@“%。3f”等等。

Hope this helps!

希望这可以帮助!

#2


32  

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. It might be good enough in some cases, but you do need to keep in mind that the result is not really rounded.

无论如何,这个故事的寓意是圆形(f * 100)/ 100.0这样的事情大约只有几轮。在某些情况下它可能已经足够好了,但你需要记住结果并不是真正的四舍五入。

If you want something better, you'll need to use decimal arithmetic instead. You can either do this by keeping your values as integers (e.g. 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.

如果你想要更好的东西,你需要使用十进制算术。您可以通过将值保持为整数来实现此目的(例如,对于货币值,将它们保持为便士或美分而不是磅或美元),或者使用*的十进制浮点页面上可以找到的各种十进制浮点包之一。

#3


1  

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 ^(小数位数)。

#4


1  

I just post my answer to this question cause it was the only way for me to get this working quickly as I merged two of the given answers to display a rounded float value to the user in an iPad app:

我只是发布了我对这个问题的回答,因为这是让我快速完成工作的唯一方法,因为我合并了两个给定的答案,在iPad应用中向用户显示一个圆形浮点值:

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

#1


39  

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. If you want three decimal places put @"%.3f" and so on.

@“%。2f”将舍入到小数点后两位。如果你想要三个小数位放@“%。3f”等等。

Hope this helps!

希望这可以帮助!

#2


32  

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. It might be good enough in some cases, but you do need to keep in mind that the result is not really rounded.

无论如何,这个故事的寓意是圆形(f * 100)/ 100.0这样的事情大约只有几轮。在某些情况下它可能已经足够好了,但你需要记住结果并不是真正的四舍五入。

If you want something better, you'll need to use decimal arithmetic instead. You can either do this by keeping your values as integers (e.g. 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.

如果你想要更好的东西,你需要使用十进制算术。您可以通过将值保持为整数来实现此目的(例如,对于货币值,将它们保持为便士或美分而不是磅或美元),或者使用*的十进制浮点页面上可以找到的各种十进制浮点包之一。

#3


1  

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 ^(小数位数)。

#4


1  

I just post my answer to this question cause it was the only way for me to get this working quickly as I merged two of the given answers to display a rounded float value to the user in an iPad app:

我只是发布了我对这个问题的回答,因为这是让我快速完成工作的唯一方法,因为我合并了两个给定的答案,在iPad应用中向用户显示一个圆形浮点值:

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