Decimal house - JQuery

2

Good afternoon! The code below performs the calculation of the mean, however, depending on the value it gets periodic decimal, how to round the values in decimal places, for example 0.00 or 00.00? Thank you.

HTML:

Nota 1: <input class="nota" /><br/>
Nota 2: <input class="nota" /><br/>
Nota 3: <input class="nota" /><br/><br/>
Media: <input id="media" readonly/>

JQUERY:

$(function(){
$('.nota').change(function(){
  var total = 0;
  var notas = 0;
  $('.nota').each(function(){
      var nota = new Number(this.value);          
      if(nota === 0) return;          
      total = total + nota;
      notas++;
  })
  $('#media').val(total/notas);
  });
})
    
asked by anonymous 25.04.2016 / 21:35

1 answer

2

You can use .toFixed (2)

var num = 5.56789;
var n = num.toFixed(2);
//5.57
    
27.04.2016 / 15:18