价格除以3 [英] Dividing prices by 3
问题描述
有关会计程序,我需要3所以可以共享超过3个月
For accounting program I need to divide a price by 3 so it can be shared over 3 months
例如9€
- 3€正月
- 3€第二个月
- 3€第三个月
现在,这将是价格/ 3
Now this would be price/3
但如果人数是10?
- 3,33€正月
- 3,33€第二个月
- 3,33€上个月
3,33€* 3 =€9.99加入
3,33€*3 =€9.99
一分钱不见了。
我怎样才能使它所以输出中会变得3,33€,3,33€,3,34€?
One cent has gone missing. How can I make it so the ouput would become 3,33€ , 3,33€ , 3,34€?
推荐答案
作为拔示巴说,首先问你的用户。
As Bathsheba said, ask your users first.
下面是我用过经常在这样的情况下的一种技术。这种方法将确保最均匀分布,朝向末端的向上偏压。例如,如果你调用 DivvyUp(101,3)
,您将获得 33.66,33.67,33.67
。注意,所不同的是不正好弥补了在末端。相反,每个值是根据什么剩下来计算的,而不是启动时使用。
Here's a technique that I've used often in such scenarios. This method will ensure the most even distribution, with the upward bias toward the end. For example, if you call DivvyUp(101, 3)
, you'll get 33.66, 33.67, 33.67
. Notice that the difference isn't just made up for at the end. Instead, each value is computed according to what's left, not what was started with.
public static double[] DivvyUp(double total, uint count)
{
var parts = new double[count];
for (var i = 0; i < count; ++i)
{
var part = Math.Truncate((100d * total) / (count - i)) / 100d;
parts[i] = part;
total -= part;
}
return parts;
}
这篇关于价格除以3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!