Skip to content

Instantly share code, notes, and snippets.

@hesenger
Last active January 12, 2019 11:48
Show Gist options
  • Save hesenger/66cd225fe8731fe63c60241ef229a24d to your computer and use it in GitHub Desktop.
Save hesenger/66cd225fe8731fe63c60241ef229a24d to your computer and use it in GitHub Desktop.
JS - Divide um valor monetário em parcelas iguais, com eventual diferença na primeira parcela.
(function () {
var trunc = function (vl) {
return Math.trunc(vl * 100) / 100;
};
var func = function (valor, parcelas) {
if (parcelas === 1)
return [valor];
var integ = Math.trunc(valor * 100);
var vl = integ / parcelas;
vl = trunc(vl / 100);
var arr = [];
var total = 0;
for (var i = 0; i < parcelas; i++) {
arr.push(vl);
total += vl;
}
arr[0] += trunc(valor - total);
return arr;
};
window.parcelar = func;
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment