Trying to come up with a function to correctly split a given number into n parts, with each part split by percentage.
Let’s say I need to split 12
into 3 parts. The first part should be 50%
of 6, second 30%
of 6 and third 20%
of 6
. With rounding, this would result in:
[6,5,2]
totalling 13
.
Is there a way to run this calculation so it can correctly split 12
into 3 parts by the %, whereby it reduces the last item in the array by 1, then the second to last item in the array by 1 and so on until all 3 numbers total 12
? In the example above, the end result would be [6,5,1]
.
Trying to avoid calculating the 3 parts individually then calculating the difference to manually reduce each, so attempting to work out a function to handle it.