PHPackages                             kschu91/largest-remainder-method - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. kschu91/largest-remainder-method

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

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.

v1.1(4y ago)1182.2k↓64.1%5[1 issues](https://github.com/kschu91/largest-remainder-method/issues)[3 PRs](https://github.com/kschu91/largest-remainder-method/pulls)1PHPPHP &gt;=7.3CI failing

Since Oct 7Pushed 5mo ago1 watchersCompare

[ Source](https://github.com/kschu91/largest-remainder-method)[ Packagist](https://packagist.org/packages/kschu91/largest-remainder-method)[ RSS](/packages/kschu91-largest-remainder-method/feed)WikiDiscussions master Synced 3d ago

READMEChangelog (4)Dependencies (2)Versions (6)Used By (1)

[![Build Status](https://camo.githubusercontent.com/d0092c421e43289754b6d44a60028a706a49e37c1077cf6542a7cf6b5a32c23d/68747470733a2f2f6170692e7472617669732d63692e636f6d2f6b7363687539312f6c6172676573742d72656d61696e6465722d6d6574686f642e7376673f6272616e63683d6d6173746572267374617475733d706173736564)](https://travis-ci.com/kschu91/largest-remainder-method)[![Code Coverage](https://camo.githubusercontent.com/6a4f4516a0a00f96cbd1fcc58ebf9f82ee3f67b62b8decf8ac0042ad1154d99d/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6b7363687539312f6c6172676573742d72656d61696e6465722d6d6574686f642f6261646765732f636f7665726167652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/kschu91/largest-remainder-method/?branch=master)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/e113162ba03d8ebef19020dff29eb60e7072a06bc95166dc244f4baf361f5deb/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6b7363687539312f6c6172676573742d72656d61696e6465722d6d6574686f642f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/kschu91/largest-remainder-method/?branch=master)

largest remainder method algorithm
==================================

[](#largest-remainder-method-algorithm)

A PHP implementation of the [largest remainder method](https://en.wikipedia.org/wiki/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
-----------

[](#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
------------

[](#installation)

```
composer require "kschu91/largest-remainder-method"
```

If you are not familiar with composer: [composer basic usage](https://getcomposer.org/doc/01-basic-usage.md)

### Requirements

[](#requirements)

- PHP &gt;= 7.3

Basic Usage
-----------

[](#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
------------------------------------

[](#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
-----------------------------------

[](#working-with-complex-arraysobjects)

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
        )

)

```

###  Health Score

44

—

FairBetter than 90% of packages

Maintenance47

Moderate activity, may be stable

Popularity39

Limited adoption so far

Community12

Small or concentrated contributor base

Maturity61

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~396 days

Total

4

Last Release

1636d ago

Major Versions

v0.2 → v1.02021-06-23

PHP version history (2 changes)v0.1PHP ^7.1

v1.0PHP &gt;=7.3

### Community

Maintainers

![](https://www.gravatar.com/avatar/16a030944fdb6c9508d602db24db8f25023b76393295a19d206e586e686611b8?d=identicon)[kschu91](/maintainers/kschu91)

---

Top Contributors

[![kschu91](https://avatars.githubusercontent.com/u/5566756?v=4)](https://github.com/kschu91 "kschu91 (24 commits)")

---

Tags

algorithmmathphpphp-libraryrounding

###  Code Quality

TestsPHPUnit

Code StylePHP\_CodeSniffer

### Embed Badge

![Health badge](/badges/kschu91-largest-remainder-method/health.svg)

```
[![Health](https://phpackages.com/badges/kschu91-largest-remainder-method/health.svg)](https://phpackages.com/packages/kschu91-largest-remainder-method)
```

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
