{"id":18638859,"url":"https://github.com/kschu91/largest-remainder-method","last_synced_at":"2025-09-03T05:39:13.463Z","repository":{"id":42172772,"uuid":"151945140","full_name":"kschu91/largest-remainder-method","owner":"kschu91","description":"A PHP implementation of the largest remainder method algorithm.","archived":false,"fork":false,"pushed_at":"2024-04-10T02:00:09.000Z","size":40,"stargazers_count":11,"open_issues_count":3,"forks_count":5,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-07-12T23:43:13.231Z","etag":null,"topics":["algorithm","math","php","php-library","rounding"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/kschu91.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-10-07T13:37:42.000Z","updated_at":"2023-11-14T13:14:23.000Z","dependencies_parsed_at":"2024-06-21T18:55:43.670Z","dependency_job_id":"82d2996d-0d92-4154-805e-61a23187d552","html_url":"https://github.com/kschu91/largest-remainder-method","commit_stats":{"total_commits":24,"total_committers":1,"mean_commits":24.0,"dds":0.0,"last_synced_commit":"205b7c5b5376a6b7fbf8a7a6370c35e12cdc6619"},"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"purl":"pkg:github/kschu91/largest-remainder-method","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kschu91%2Flargest-remainder-method","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kschu91%2Flargest-remainder-method/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kschu91%2Flargest-remainder-method/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kschu91%2Flargest-remainder-method/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/kschu91","download_url":"https://codeload.github.com/kschu91/largest-remainder-method/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kschu91%2Flargest-remainder-method/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":266761515,"owners_count":23980300,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-07-23T02:00:09.312Z","response_time":66,"last_error":null,"robots_txt_status":null,"robots_txt_updated_at":null,"robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["algorithm","math","php","php-library","rounding"],"created_at":"2024-11-07T05:44:03.673Z","updated_at":"2025-07-23T22:39:11.084Z","avatar_url":"https://github.com/kschu91.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![Build Status](https://api.travis-ci.com/kschu91/largest-remainder-method.svg?branch=master\u0026status=passed)](https://travis-ci.com/kschu91/largest-remainder-method)\n[![Code Coverage](https://scrutinizer-ci.com/g/kschu91/largest-remainder-method/badges/coverage.png?b=master)](https://scrutinizer-ci.com/g/kschu91/largest-remainder-method/?branch=master)\n[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/kschu91/largest-remainder-method/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/kschu91/largest-remainder-method/?branch=master)\n\n# largest remainder method algorithm\n\nA 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.\n\n## The problem\n\nAssume the following example:\n```\n18.562874251497007%\n20.958083832335326%\n18.562874251497007%\n19.161676646706585%\n22.75449101796407%\n```\nWhen rounding the above percentages using PHP´s rounding functions, we get:\n\n```\n19%\n21%\n19%\n19%\n23%\n```\n\nWhich in fact sums up to `101%` instead of `100%`. The largest remainder method solves this issue by doing the following steps:\n\n1. Rounding all values down to the nearest integer value\n2. Determining the difference between the sum of the rounded values and total value\n3. Distributing the difference between the rounded values in decreasing order of their decimal parts\n\n## Installation\n\n```bash\ncomposer require \"kschu91/largest-remainder-method\"\n```\n\nIf you are not familiar with composer:\n[composer basic usage](https://getcomposer.org/doc/01-basic-usage.md)\n\n### Requirements\n- PHP \u003e= 7.3\n\n## Basic Usage\n\n```php\n$numbers = [\n    18.562874251497007,\n    20.958083832335326,\n    18.562874251497007,\n    19.161676646706585,\n    22.75449101796407\n];\n\n$lr = new LargestRemainder($numbers);\n\nprint_r($lr-\u003eround());\n```\nwhich results in:\n```\nArray\n(\n    [0] =\u003e 19\n    [1] =\u003e 21\n    [2] =\u003e 18\n    [3] =\u003e 19\n    [4] =\u003e 23\n)\n```\n\n## Working with decimals aka. precision\nThe default precision is set to `0`. But you can change this behaviour by using the `setPrecision` method:\n```php\n$numbers = [\n    18.562874251497007,\n    20.958083832335326,\n    18.562874251497007,\n    19.161676646706585,\n    22.75449101796407\n];\n\n$lr = new LargestRemainder($numbers);\n$lr-\u003esetPrecision(2);\n\nprint_r($lr-\u003eround());\n```\nwhich results in:\n```\nArray\n(\n    [0] =\u003e 18.56\n    [1] =\u003e 20.96\n    [2] =\u003e 18.56\n    [3] =\u003e 19.16\n    [4] =\u003e 22.76\n)\n```\n\n## Working with complex arrays/objects\nMostly, 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.\nThat´s why this library also supports callbacks for applying this algorithm.\n\nYou 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.\n\n\u003e Make sure to pass the first argument of the setter callback as reference, eg. as in the example below: `\u0026$item`. If not, the resulting data will maintain their original numbers and are not rounded.\n\n```php\n$objects = [\n    ['a' =\u003e 18.562874251497007],\n    ['a' =\u003e 20.958083832335326],\n    ['a' =\u003e 18.562874251497007],\n    ['a' =\u003e 19.161676646706585],\n    ['a' =\u003e 22.75449101796407]\n];\n\n$lr = new LargestRemainder($objects);\n$lr-\u003esetPrecision(2);\n\nprint_r($lr-\u003euround(\n    function ($item) {\n        return $item['a'];\n    },\n    function (\u0026$item, $value) {\n        $item['a'] = $value;\n    }\n));\n```\n\nwhich results in:\n```\nArray\n(\n    [0] =\u003e Array\n        (\n            [a] =\u003e 18.55\n        )\n\n    [1] =\u003e Array\n        (\n            [a] =\u003e 20.94\n        )\n\n    [2] =\u003e Array\n        (\n            [a] =\u003e 18.55\n        )\n\n    [3] =\u003e Array\n        (\n            [a] =\u003e 19.15\n        )\n\n    [4] =\u003e Array\n        (\n            [a] =\u003e 22.74\n        )\n\n)\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkschu91%2Flargest-remainder-method","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkschu91%2Flargest-remainder-method","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkschu91%2Flargest-remainder-method/lists"}