I'm working with values ( decimal(18,2)
) of a sale where the sum of the price of the products should turn into a certain number of parcels. So that I can divide the total of the products exactly for the parcels, I also have to calculate the remainder of the division and then apply this leftover to a single final parcel. For this I do:
var parcelas = 3;
var produtos = [
{nome: 'bola', valor: 10},
{nome: 'pipa', valor: 5.3},
{nome: 'carro', valor: 15}
];
//total dos produtos (resultado = 30.3 ~> R$30,30)
var total = 0;
for(var i in produtos){
total = total + produtos[i].valor;
}
//verifico se há resto na divisão
var restoDivisao = total % parcelas
When I check for the rest in the split, it is returning 0.30
but if I split 30.3 / 3
the result is 10.1
.
What is the correct way for me to check for a remainder in a division with decimal values?
Testing, in a way I was able to do this:
var restoDivisao = (((total) * 100) % parcelas) / 100;
This works, but I can not really accept that there is no "cleaner" method. Is there a more visually correct way to get to the rest of this division?