php中使用最大余额法解决百分比计算相加不等于100%的问题

19 篇文章 1 订阅
5 篇文章 0 订阅

前言

在开发项目的过程中有时候需要进行计算百分比,例如计算饼状图百分比。有时候在计算的过程中常规四舍五入计算会发生所有计算的值相加不等于100%的情况。
33.33% + 33.33% + 33.33% = 99.99%
为了避免在数据计算过程中出现这种误差,下面介绍一种计算数据的方法:

if (!function_exists('get_percent_value')) {
    /**
     * 最大余额法,解决百分比计算相加不等于100%(扇形/饼图百分比使用的此算法)
     * @param array $valueList 二维数组 [['value' => 1],['value' => 2],['value' => 3]]
     * @param string $contKey 要统计的字段
     * @param int $precision 精度(默认为2保留百分比格式的两位小数)
     * @param string $percentKey 百分比键名
     * @param bool $format 是否需要返回格式化后百分比格式,false则返回小数
     * @return array
     */
    function get_percent_value(array $valueList, string $contKey, int $precision = 2, string $percentKey = 'percent', bool $format = true): array
    {
        if (count($valueList) === 0) {
            return [];
        }
        // 求和
        $sum = array_sum(array_column($valueList, $contKey));
        // 为0直接返回
        if ($sum == 0) {
            foreach ($valueList as $k => $v) {
                $valueList[$k][$percentKey] = 0;
                // 格式化为百分比数据格式
                if ($format) {
                    $valueList[$k][$percentKey] = '0%';
                }
            }
            return $valueList;
        }
        // 10的2次幂是100,用于计算精度
        $digits = pow(10, $precision);
        $currentSum = 0;
        $remainder = [];
        foreach ($valueList as $k => $v) {
            $votesPerQuota = $v[$contKey] / $sum * $digits * 100;
            $valueList[$k]['integer'] = (int) $votesPerQuota;
            // 余数
            $remainder[$k] = $votesPerQuota - $valueList[$k]['integer'];
            // 整数总数(可能会小于真正总数)
            $currentSum += $valueList[$k]['integer'];
        }
        $targetSeats = $digits * 100;
        // 找到最大的余数对其整数部分加1,相同则对找到的第一个加1,凑占比100%,不足100%则继续循环,并把已经加1的从列表中去除
        while ($currentSum < $targetSeats) {
            $key = array_search(max($remainder), $remainder);
            $valueList[$key]['integer']++;
            unset($remainder[$key]);
            $currentSum++; // 对总数也加1
        }
        foreach ($valueList as $k => $v) {
            $valueList[$k][$percentKey] = bcdiv((string) $v['integer'], (string) $targetSeats, $precision + 2);
            // 格式化为百分比数据格式
            if ($format) {
                $valueList[$k][$percentKey] = bcmul($valueList[$k][$percentKey], "100", $precision) . '%';
            }
            unset($valueList[$k]['integer']);
        }
        return $valueList;
    }
}

使用方法可以直接进行调用。

$data = [
    [
        'value' => 3,
    ],
    [
        'value' => 3,
    ],
    [
        'value' => 3,
    ],
];

$rate_data = get_percent_value($data, 'value', 2, 'percent', false);

//输出结果为
[         
  [  
    "value" => 3,    
    "percent" => "0.3334"
  ],
  [
    "value" => 3,
    "percent" => "0.3333"
  ],
  [
    "value" => 3,
    "percent" => "0.3333"
  ]
]

这样经过调用方法进行数据计算就不会出现计算结果相加不等于100%的情况了。

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值