I'm doing a function of product calculations. In the following scenarios:
- User can add
- User can give discount (discount on value + addition)
- User can give partner discount (discount on original value)
Function example:
function calcularValor(valorOriginal, acrescimo, desconto, descontoParceiro)
{
var valorAcrescimo = (valorOriginal / ((100 - acrescimo) / 100)) - valorOriginal ;
var valorDesconto = ((valorOriginal + valorAcrescimo) / 100) * desconto;
var valorDescontoParceiro = (valorOriginal / 100) * descontoParceiro;
return valorOriginal + valorAcrescimo - valorDesconto - valorDescontoParceiro;
}
But the problem is this: When I save the value it is not hitting 1 cent (it is not significant for 1 request, but for 1000 requests it is already making a nice difference), because I save valorAcrescimo
, valorDesconto
, valorDescontoParceiro
to 2 decimal places.
How would you work around this problem?
Do the calculation and already round the value and use to do the other calculation? (According to valorAcrescimo
)