PHPackages                             romainnorberg/residue - 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. romainnorberg/residue

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

romainnorberg/residue
=====================

Divide a float into several parts, with distribution of any remainder

v0.4(2y ago)8401MITPHPPHP ^8.2

Since Sep 15Pushed 2y ago2 watchersCompare

[ Source](https://github.com/romainnorberg/residue)[ Packagist](https://packagist.org/packages/romainnorberg/residue)[ GitHub Sponsors](https://github.com/romainnorberg)[ RSS](/packages/romainnorberg-residue/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (6)Dependencies (4)Versions (7)Used By (0)

Residue ⚖️
==========

[](#residue-️)

 [![Latest Version on Packagist](https://camo.githubusercontent.com/a4abc7918f7945c396da136435bf41049dcf1803a2f2ed15c0f97a9182da44ae/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f726f6d61696e6e6f72626572672f726573696475652e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/romainnorberg/residue) [![GitHub Tests Action Status](https://camo.githubusercontent.com/a39370bfb0c593eb65b204cb8a502382c64122aed375535009385eb6361e66bd/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f776f726b666c6f772f7374617475732f726f6d61696e6e6f72626572672f726573696475652f72756e2d74657374733f6c6162656c3d7465737473)](https://github.com/romainnorberg/residue/actions?query=workflow%3Arun-tests+branch%3Amaster) [![codecov](https://camo.githubusercontent.com/5dc60eb5d957f8de077f4d578ce404931a6f5d951664b71a20b156bc7ef6f2ca/68747470733a2f2f636f6465636f762e696f2f67682f726f6d61696e6e6f72626572672f726573696475652f6272616e63682f6d61737465722f67726170682f62616467652e737667)](https://codecov.io/gh/romainnorberg/residue) [![Total Downloads](https://camo.githubusercontent.com/07228d159597d3f02019b0d63a3948ceb5640dc5c981d7f46bbcc053b59ccecd/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f726f6d61696e6e6f72626572672f726573696475652e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/romainnorberg/residue/stats)

### Divide a float into several parts, with distribution of any remainder.

[](#divide-a-float-into-several-parts-with-distribution-of-any-remainder)

[![Residue Package - Illustration credit: https://refactoring.guru/](https://raw.githubusercontent.com/romainnorberg/residue/master/.github/media/readme.jpg)](https://raw.githubusercontent.com/romainnorberg/residue/master/.github/media/readme.jpg)

#### Introduction

[](#introduction)

This dependency-free package provides a `split` method to help you split float into parts, with the possible distribution of any remainder.

It is also possible to specify a rounding of the divided amount, for example rounding by 0.05.

**[🕹 Try out Residue on the online tester! »](https://residue-online-php-tester.herokuapp.com/)**

#### Installation

[](#installation)

You can install the package via composer:

```
composer req romainnorberg/residue
```

#### Requirements

[](#requirements)

This package require &gt;= PHP 7.4

#### Residue VS Brick\\Money

[](#residue-vs-brickmoney)

This package does not deal with the notion of currency and being more basic, it is up to 40 times faster on basic operations than the [brick/money](https://github.com/brick/money) package

Benchmarks: [residue-vs-brick-money](https://github.com/romainnorberg/residue-vs-brick-money)

Usage / examples
----------------

[](#usage--examples)

#### Basic split

[](#basic-split)

```
Residue::create(100)->divideBy(3)->split(); // -> \Generator[33.34, 33.33, 33.33]

// or

Residue::create(100)->divideBy(3)->toArray(); // -> [33.34, 33.33, 33.33]
```

#### Split with rounding (and remainder)

[](#split-with-rounding-and-remainder)

```
Residue::create(100)
            ->divideBy(3)
            ->step(0.05)
            ->split(); // -> \Generator[33.35, 33.35, 33.30]
```

With remainder:

```
$r = Residue::create(7.315)
                ->divideBy(3)
                ->step(0.05);

$r->split(); // -> \Generator[2.45, 2.45, 2.40]
$r->getRemainder(); // -> 0.015
```

#### Split mode

[](#split-mode)

`SPLIT_MODE_ALLOCATE` is default mode and try to allocate the maximum of the value according to step.

```
$r = Residue::create(100)
            ->divideBy(3)
            ->decimal(0);

$r->split(); // -> \Generator[34, 33, 33]
$r->getRemainder(); // 0

//

$r = Residue::create(101)
            ->divideBy(3)
            ->decimal(0);

$r->split(); // -> \Generator[34, 34, 33]
$r->getRemainder(); // 0
```

`SPLIT_MODE_EQUITY` mode try to allocate equally the maximum of the value according to step.

```
$r = Residue::create(100)
            ->divideBy(3)
            ->decimal(0);

$r->split(Residue::SPLIT_MODE_EQUITY); // -> \Generator[33, 33, 33]
$r->getRemainder(); // 1

//

$r = Residue::create(101)
            ->divideBy(3)
            ->decimal(0);

$r->split(Residue::SPLIT_MODE_EQUITY); // -> \Generator[33, 33, 33]
$r->getRemainder(); // 2
```

#### Generator

[](#generator)

This package uses [generator](https://www.php.net/manual/en/language.generators.syntax.php) to reduce the memory used

With foreach statement (using generator):

```
$r = Residue::create(100)->divideBy(3);
foreach ($r->split() as $part) {
    var_dump($part);
}

float(33.34)
float(33.33)
float(33.33)
```

To array:

```
$r = Residue::create(100)->divideBy(3);
var_dump($r->toArray());

array(3) {
  [0]=>
  float(33.34)
  [1]=>
  float(33.33)
  [2]=>
  float(33.33)
}
```

Testing
-------

[](#testing)

```
composer test
```

Contributing
------------

[](#contributing)

Please see [CONTRIBUTING](CONTRIBUTING.md) for details.

Security
--------

[](#security)

If you discover any security related issues, please email  instead of using the issue tracker.

Credits
-------

[](#credits)

- [Romain Norberg](https://github.com/romainnorberg)
- [All Contributors](../../contributors)
- Illustration from Refactoring.Guru

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE) for more information.

###  Health Score

29

—

LowBetter than 60% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity14

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity61

Established project with proven stability

 Bus Factor1

Top contributor holds 84.6% 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 ~214 days

Recently: every ~265 days

Total

6

Last Release

989d ago

PHP version history (2 changes)v0.1-alphaPHP ^7.4

v0.4PHP ^8.2

### Community

Maintainers

![](https://www.gravatar.com/avatar/0d2089438da6aa35ffc3b53219ca96e8b0ce5a6b495cb7b56619ddc6ad50a6fd?d=identicon)[romainnorberg](/maintainers/romainnorberg)

---

Top Contributors

[![romainnorberg](https://avatars.githubusercontent.com/u/7681951?v=4)](https://github.com/romainnorberg "romainnorberg (11 commits)")[![MockingMagician](https://avatars.githubusercontent.com/u/33423809?v=4)](https://github.com/MockingMagician "MockingMagician (2 commits)")

---

Tags

phpresiduesplitphpsplitresidue

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StylePHP CS Fixer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/romainnorberg-residue/health.svg)

```
[![Health](https://phpackages.com/badges/romainnorberg-residue/health.svg)](https://phpackages.com/packages/romainnorberg-residue)
```

###  Alternatives

[imanghafoori/laravel-anypass

A minimal yet powerful package to help you in development.

21421.6k](/packages/imanghafoori-laravel-anypass)

PHPackages © 2026

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