- This topic has 7 replies, 1 voice, and was last updated 2 months, 1 week ago by MockingMagician.
- AuthorPosts
- November 10, 2020 at 7:09 pm #2018romainnorbergParticipant
Hi 👋
I am working on a PHP package which allows to split a number by having an equitable distribution with a remainder.
This remainder is:
* either distributed over the last division
* or in the case of rounding where it cannot be distributed, available via the API.Please give it a try and give me your feedback. It is a work in progress but which is already in full scale test in a project in production.
Github: [https://github.com/romainnorberg/residue](https://github.com/romainnorberg/residue)
## Usage / examples
You will find examples of basic split, with rounding and rounding with remainder: [https://github.com/romainnorberg/residue#usage–examples](https://github.com/romainnorberg/residue#usage–examples)
## Basic split
$residue = (new Residue(100))->divideBy(3)->split(); // -> 33.33, 33.33, 33.34
## Split with rounding (and remainder)
$residue = (new Residue(100))
->divideBy(3)
->step(0.05)
->split(); // -> 33.35, 33.35, 33.30With remainder:
$residue = (new Residue(7.315))
->divideBy(3)
->decimal(3)
->step(0.05)
->split(); // -> [2.45, 2.45, 2.40]
…
$residue->getStepRemainder(); // -> 0.015Thank you
November 10, 2020 at 7:09 pm #2019Flibbertygibbety22GuestThat’s a really well-coded library – easy-to-use API, tested, documented, Composer, comparison to similar libraries.
I actually have somewhere in my codebase this could be useful, except I need to weight the apportionments, and I don’t think this does that (yet)?
November 10, 2020 at 7:09 pm #2020drupolGuestNice and clean 🙂
November 10, 2020 at 7:09 pm #2021MUK99GuestThere is French documentation in residue.php on lind 69-71
November 10, 2020 at 7:09 pm #2022AegirLeetGuestYou should add a static constructor. `Residue::make(100)->divideBy(3)->split()` is a lot nicer to write than `(new Residue(100))->divideBy(3)->split()`.
November 10, 2020 at 7:09 pm #2023mYkon123Guestnice
[https://github.com/romainnorberg/residue/blob/master/src/Residue.php#L60](https://github.com/romainnorberg/residue/blob/master/src/Residue.php#L60) ?Generator return but is never null right?
November 10, 2020 at 7:09 pm #2024romainnorbergGuestHi,
Thank you all for your constructive comments!
A new version has been released with:
* a **static constructor**
>Residue::create(100)
>
>->divideBy(3)
>
>->step(0.05)
>
>->split(); // -> 33.35, 33.35, 33.30* a more **robust code** *(coverage, analysis, exception, … thanks to* u/MockingMagician)
[https://github.com/romainnorberg/residue/releases/tag/v0.3.1](https://github.com/romainnorberg/residue/releases/tag/v0.3.1)
November 10, 2020 at 7:09 pm #2025MockingMagicianGuest“No else” package. If I could, I’ll starring twice! Very nice, light package…
I agree with [**u/AegirLeet**](https://www.reddit.com/user/AegirLeet/) **,** Residue::make() should (MUST) be implemented
- AuthorPosts
- You must be logged in to reply to this topic.