Decimal place - Jquery

Asked

Viewed 1,658 times

2

Good afternoon! The code below calculates the average, however, depending on the value it is periodically decimated, how to round the values in decimals, 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);
  });
})
  • 1

    http://answall.com/q/11018/101

  • I haven’t been able to round up yet (.00), does anyone have any ideas to help me? Thanks

1 answer

3


You can use the .toFixed(2)

var num = 5.56789;
var n = num.toFixed(2);
//5.57
  • Perfect @Felipefm, Thank you!

  • @Felipe FM helped me so much, thanks.

Browser other questions tagged

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