PHP Package to divide a float into several parts, with distribution of any remainder (Residue)

Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #2018
    romainnorberg
    Participant

    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.30

    With remainder:

    $residue = (new Residue(7.315))
    ->divideBy(3)
    ->decimal(3)
    ->step(0.05)
    ->split(); // -> [2.45, 2.45, 2.40]

    $residue->getStepRemainder(); // -> 0.015

    Thank you

    #2019
    Flibbertygibbety22
    Guest

    That’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)?

    #2020
    drupol
    Guest

    Nice and clean 🙂

    #2021
    MUK99
    Guest

    There is French documentation in residue.php on lind 69-71

    #2022
    AegirLeet
    Guest

    You should add a static constructor. `Residue::make(100)->divideBy(3)->split()` is a lot nicer to write than `(new Residue(100))->divideBy(3)->split()`.

    #2023
    mYkon123
    Guest

    nice

    [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?

    #2024
    romainnorberg
    Guest

    Hi,

    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)

    #2025
    MockingMagician
    Guest

    “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

Viewing 8 posts - 1 through 8 (of 8 total)
  • You must be logged in to reply to this topic.