承接 kschu91/largest-remainder-method 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

kschu91/largest-remainder-method

Composer 安装命令:

composer require kschu91/largest-remainder-method

包简介

A PHP implementation of the largest remainder method algorithm. This method is the most common way to get rid of rounding issues when working with rounded percentage values.

README 文档

README

Build Status Code Coverage Scrutinizer Code Quality

largest remainder method algorithm

A PHP implementation of the largest remainder method algorithm. This method is the most common way to get rid of rounding issues when working with rounded percentage values.

The problem

Assume the following example:

18.562874251497007%
20.958083832335326%
18.562874251497007%
19.161676646706585%
22.75449101796407%

When rounding the above percentages using PHP´s rounding functions, we get:

19%
21%
19%
19%
23%

Which in fact sums up to 101% instead of 100%. The largest remainder method solves this issue by doing the following steps:

  1. Rounding all values down to the nearest integer value
  2. Determining the difference between the sum of the rounded values and total value
  3. Distributing the difference between the rounded values in decreasing order of their decimal parts

Installation

composer require "kschu91/largest-remainder-method"

If you are not familiar with composer: composer basic usage

Requirements

  • PHP >= 7.3

Basic Usage

$numbers = [
    18.562874251497007,
    20.958083832335326,
    18.562874251497007,
    19.161676646706585,
    22.75449101796407
];

$lr = new LargestRemainder($numbers);

print_r($lr->round());

which results in:

Array
(
    [0] => 19
    [1] => 21
    [2] => 18
    [3] => 19
    [4] => 23
)

Working with decimals aka. precision

The default precision is set to 0. But you can change this behaviour by using the setPrecision method:

$numbers = [
    18.562874251497007,
    20.958083832335326,
    18.562874251497007,
    19.161676646706585,
    22.75449101796407
];

$lr = new LargestRemainder($numbers);
$lr->setPrecision(2);

print_r($lr->round());

which results in:

Array
(
    [0] => 18.56
    [1] => 20.96
    [2] => 18.56
    [3] => 19.16
    [4] => 22.76
)

Working with complex arrays/objects

Mostly, you don´t have the numbers you want to apply this algorithm on in a simple array as in the examples above. You rather have them in objects or associative arrays. That´s why this library also supports callbacks for applying this algorithm.

You just have to supply 2 callbacks to the usort method. The first one, to fetch the relevant number from the object. And the second one to write the rounded number back to the resulting object.

Make sure to pass the first argument of the setter callback as reference, eg. as in the example below: &$item. If not, the resulting data will maintain their original numbers and are not rounded.

$objects = [
    ['a' => 18.562874251497007],
    ['a' => 20.958083832335326],
    ['a' => 18.562874251497007],
    ['a' => 19.161676646706585],
    ['a' => 22.75449101796407]
];

$lr = new LargestRemainder($objects);
$lr->setPrecision(2);

print_r($lr->uround(
    function ($item) {
        return $item['a'];
    },
    function (&$item, $value) {
        $item['a'] = $value;
    }
));

which results in:

Array
(
    [0] => Array
        (
            [a] => 18.55
        )

    [1] => Array
        (
            [a] => 20.94
        )

    [2] => Array
        (
            [a] => 18.55
        )

    [3] => Array
        (
            [a] => 19.15
        )

    [4] => Array
        (
            [a] => 22.74
        )

)

kschu91/largest-remainder-method 适用场景与选型建议

kschu91/largest-remainder-method 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 81.98k 次下载、GitHub Stars 达 11, 最近一次更新时间为 2018 年 10 月 07 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 kschu91/largest-remainder-method 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 kschu91/largest-remainder-method 我们能提供哪些服务?
定制开发 / 二次开发

基于 kschu91/largest-remainder-method 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 81.98k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 12
  • 点击次数: 31
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 11
  • Watchers: 1
  • Forks: 5
  • 开发语言: PHP

其他信息

  • 授权协议: Unknown
  • 更新时间: 2018-10-07