加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php – 速率结构的数据库/算法

发布时间:2020-12-13 17:02:28 所属栏目:PHP教程 来源:网络整理
导读:我必须根据这些方面的费率结构计算价格: $303.00 fixed price up to 500 units$0.023 additional per unit from 501-10,000 units$0.022 additional per unit from 10,001-25,000 units$0.021 additional per unit from 25,001-50,000 units 我在设置数据库
我必须根据这些方面的费率结构计算价格:

$303.00 fixed price up to 500 units
$0.023 additional per unit from 501-10,000 units
$0.022 additional per unit from 10,001-25,000 units
$0.021 additional per unit from 25,001-50,000 units

我在设置数据库结构和算法(更大的粘滞点)时有点迷失了.有没有人这样做过?有没有一种漂亮,优雅的方式来计算这种东西?

编辑:例如,前500单位的25,100单位运行费用为303.00美元,接下来的9,500单位为218.50美元,接下来的15,000单位为330.00美元,接下来的100单位为2.10美元,总计853.60美元.

这不是一个简单的25,100 * $0.021计算 – 我很清楚如何选择和计算它.

与所得税的评估方式类似 – 在边际基础上.

解决方法

我假设你想要一些灵活的东西,否则硬编码会很简单.

您可以使用定价表:

ID MAX    FIX    UNIT
1  500    303    0
2  9500   0      .23
3  15000  0      .22
4  25000  0      .21

然后你可以计算如下:

$items = ?;
$cost = 0;
$rows = get_rows("select max,fix,unit from pricing order by id asc");
foreach ($rows as $r)
{
    if ($items <= 0)
        break;
    $cost += $r['fix'] + min($r['max'],$items) * $r['unit'];
    $items -= $r['max'];
}

我假设您想要PHP中的算法.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读