-2

I want to round the up my total til two decimal places.

I have this:

    var updateTotal = function () {
      var people = parseInt($('#people').val());
      var bill = parseInt($('#bill').val());
      var tip = parseInt($('#tip').val());
      var billTip = bill + tip;




      $('#total').text(billTip / people);

and i've also found this snippet to help round up but i cant seem to get my head around how to implement it.

    var rounded = Math.round((10 / 3) * 100) / 100;

Thanks

Liam
  • 27,717
  • 28
  • 128
  • 190
Ma9ic
  • 1,107
  • 4
  • 16
  • 30

4 Answers4

4

It's already implemented for you. Substitue (10 / 3) for your own variables. All it's doing is shifting the decimal place two places to the right (by multiplying by 100), rounding, then shifting it two left (by dividing by 100).

var rounded = Math.round((billTip / people) * 100) / 100;
user229044
  • 232,980
  • 40
  • 330
  • 338
3

You can also use .toFixed

$('#total').text((billTip / people).toFixed(2));
Adil Shaikh
  • 44,509
  • 17
  • 89
  • 111
0

I would use parseFloat on both your numbers, or else it will round to 00:

$('#total').text(parseFloat(billTip / people).toFixed(2));
tymeJV
  • 103,943
  • 14
  • 161
  • 157
0

You can use ceil function Math.ceil(billTip)

and for refernce you can also visit below link

http://www.w3schools.com/jsref/jsref_ceil.asp

Liam
  • 27,717
  • 28
  • 128
  • 190
Anil Gupta
  • 632
  • 4
  • 16