Round result in script

Asked

Viewed 56 times

3

In this my script how can I round up my result? I’m trying to use the (total.toFixed(2)); more 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;
}

1 answer

7


In your code total is a certain element? then you can’t do total.toFixed(2). How much you can do

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

But the best thing would be to do before you write 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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.