How do I format the value of two input in the currency format by rounding the values.
For example, if the commission is 1200 the input return ir
and valor líquido
give 18% and 1,182, but when it is a broken value, for example 22,051.56, the result is 330.77340000000004% and 21,720,787 , would have to show 330.77 and 21.720,79. Is there any way to format this kind of result in javascript or some other way?
var inputs = document.querySelectorAll('form input');
var valor = inputs[0];
var ir = inputs[1];
var liquido = inputs[2];
function formatar$(nr) {
return nr.toLocaleString('pt-BR');
}
valor.addEventListener('keyup', function() {
var nr = Number(this.value);
var _ir = nr * 1.5 / 100;
ir.value = _ir + '%';
liquido.value = formatar$(nr - _ir);
});
<form method="post" action="processa.php">
<label>Valor comissão:</label>
<input type="text" name="valor_comissao">
<label>IR:</label>
<input type="text">
<label>Valor Líquido:</label>
<input type="text">
<button type="submit">Enviar</button>
</form>