Round result in script

3

In my script how can I round up my result? I'm trying to use (total.toFixed(2)); but it does not work.

<input type="text" name="total" id="total" value="resultado" />

function updateValue(){
    //atualiza os valores
    inputQtd = parseFloat(document.getElementById("qtd").value);
    inputValor = parseFloat(document.getElementById("valor").value);
    inputMark = parseFloat(document.getElementById("mark").value);

    //atualiza o valor no resultado
    var total = document.getElementById("total");

    total.value = (inputQtd * inputValor ) / inputMark;
}
    
asked by anonymous 23.09.2015 / 17:41

1 answer

7

Is your code total a right element? then you can not total.toFixed(2) . How much you can do

total.value = total.value.toFixed(2)

But the best thing would be to do before writing in the DOM:

 total.value = ((inputQtd * inputValor ) / inputMark).toFixed(2);

Example:

var inputQtd = 145.3445;
var inputValor = 45.45;
var inputMark = 5.65;

var numero = ((inputQtd * inputValor ) / inputMark).toFixed(2);
alert(numero); // dá 1169.19
    
23.09.2015 / 17:57