Largest Remainder Round
Add a spread
function to the array object which "spread" the number array in such way that the total amount is exact without changing the percentage of each element. The "spread" action uses the Largest Remainder Method.
spread
is useful when you want to get the percentage of a number array and keep the total percentage at 100.
Installation
a) Use the Node package manager npm to install.
npm i largest-remainder-round
b) Use CDN
<script src="https://unpkg.com/largest-remainder-round"></script>
Usage
; var arr = 1000100050001000 arr // returns [13, 13, 62, 12]arr // returns [7, 6, 31, 6]arr // returns [125, 125, 625, 125] //second parameters is for number of digits after decimal placearr // returns [6.25, 6.25, 31.25, 6.25 ]arr // return [12.5, 12.5, 62.5, 12.5] //IF the input array is an obj array, then you need to put in the configurations var ObjArr = name: "Batman" vote: 1000 name: "Superman" vote: 1000 name: "Wonder Woman" vote: 5000 name: "The Flash" vote: 1000 ; ObjArr/* returns[ { name: 'Batman', vote: 1000, percentage: 6.25 }, { name: 'Superman', vote: 1000, percentage: 6.25 }, { name: 'Wonder Woman', vote: 5000, percentage: 31.25 }, { name: 'The Flash', vote: 1000, percentage: 6.25 }]*/ //The obj array config need to know at least which 'prop'perty to use for the calculationObjArr/* returns[ { name: 'Batman', vote: 1000, vote_spread: 13 }, { name: 'Superman', vote: 1000, vote_spread: 13 }, { name: 'Wonder Woman', vote: 5000, vote_spread: 62 }, { name: 'The Flash', vote: 1000, vote_spread: 12 }]*/
Contributing
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
Please make sure to update tests as appropriate.