计算不同级别的收入份额 [英] Calculating revenue share at different tiers
问题描述
我正在尝试编写一个可以计算不同层级收入的函数...例如,您卖出了价值10000美元的小部件.从$ 1-$ 999的任何收益中,我们占50%,从$ 1000-$ 4999的任何收益中,我们占25%,从$ 5000-$ 9999的收益中,我们占10%,$ 10000-$ 19999.
I'm trying to write a function that can calculate revenue at different tiered levels... For example you sell $10000 worth of widgets. Any earnings from $1 - $999 we take 50%, $1000 - $4999 we take 25%, $5000 - $9999 we take 10%, $10000 - $19999 5%.
不基于最终值获取百分比.因此,如果您赚了10000美元,您不仅会跌入10%的列.您的前$ 1000需加收50%,接下来的$ 5000需加收25%,接下来的$ 4000需加收10%.
The percentage is not taken based on the final value. So if you earned $10000 you don't just fall into the 10% column. Your first $1000 is subject to 50%, the next $5000 25%, the next $4000 10%.
谢谢.
**最终工作代码.感谢您的帮助!
**Final working code. Thanks for the help!
$input = 10000;
$tiers = array('1000' => .5, '5000' => .25, '10000' => .1, '20000' => .05, '22000' => .01);
$accounted_for = 0;
$total_share = 0;
$tier = each($tiers);
while($accounted_for < $input) {
$cutoff = $tier[0];
$percent = $tier[1];
$level = (min($cutoff, $input) - $accounted_for) * $percent;
$total_share += $level;
$accounted_for = $cutoff;
$tier = each($tiers);
}
return $total_share;
推荐答案
由于您尚未指定语言,因此这里有一些伪代码:
Since you haven't specified a language, here's some pseudocode:
input = 99999
tiers = [list of (%,cutoff) tuples]
accounted_for = 0
total_share = 0
while accounted_for is less than input:
percent,cutoff = next element of tiers
total_share += (min(cutoff,input) - accounted_for) * percent
accounted_for = cutoff
return total_share
这篇关于计算不同级别的收入份额的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!