如何处理四舍五入百分比的总和不是 100? [英] How to deal with the sum of rounded percentage not being 100?

查看:999
本文介绍了如何处理四舍五入百分比的总和不是 100?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我们有一个整数列表:

Suppose we have a list of items with an integer:

USA:       3 people
Australia: 2 people
Germany:   2 people

如果我们计算每个值占整个列表总和的百分比,我们得到:

If we calculate the percentage of each value against the sum over the whole list, we get:

USA:       3/(3+2+2)*100 = 42.857...%
Australia: 2/(3+2+2)*100 = 28.571...%
Germany:   2/(3+2+2)*100 = 28.571...%

如果我们四舍五入,我们得到:

and if we round it, we get:

USA:       43%
Australia: 29%
Germany:   29%

和 43+29+29 = 101 不是 100,对于软件的用户来说看起来有点奇怪.你会如何解决这个问题?

The sum 43+29+29 = 101 is not 100, and it looks a little bit strange to the user of the software. How would you solve this problem?

推荐答案

Option 1

如果您担心结果对用户来说有点奇怪,我会在结果中添加一个脚注,指出百分比已四舍五入,可能不会总计为 100%.只有在舍入导致此行为时,您才能以编程方式显示消息.

Option 1

If you are concerned about the results looking a bit strange to the user, I would put a footnote regarding the results mentioning that percentages have been rounded and may not total to 100%. You could programmatically display the message only when the rounding causes this behavior.

USA percentage:       43
Australia percentage: 29
Germany percentage:   29

*由于四舍五入,百分比总和可能不是 100

由于您使用的是 Ruby,我建议您使用 rational数字.这样您就不会在需要时失去精度.您可以不使用脚注,而是显示百分比,旁边有有理数,如下所示:

Since you are using Ruby, I would suggest using rational numbers. This way you don't lose the precision when needed. Instead of the footnote, you might display the percentage with the rational numbers next to it like the following:

USA percentage:       43 (3/7)
Australia percentage: 29 (2/7)
Germany percentage:   29 (2/7)

选项 3

包括更多的小数位,以便舍入误差不那么严重:

Option 3

Include more decimal places so that the rounding error is less severe:

USA percentage:       42.9
Australia percentage: 28.6
Germany percentage:   28.6

结果是 100.1 而不是 101.

This results in 100.1 instead of 101.

这篇关于如何处理四舍五入百分比的总和不是 100?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆