Last active
January 12, 2019 11:48
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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