0

I am practicing XHTML with JavaScript. I want the cost to be in two decimal places, just like how we calculate money in real life. How would I do that? This is my code in the JavaScript file:

function computeCost() {
    var french = document.getElementById("french").value;
    var hazlenut = document.getElementById("hazlenut").value;
    var colombian = document.getElementById("colombian").value;
    var regular = document.getElementById("regular").value;

//Compute the cost
    document.getElementById("cost").value =
    totatCost = french * 3.49 + hazlenut * 3.95 + colombian * 4.59 
            + regular * 1.99;
} // end computeCost

1 Answers1

0

Use toFixed(), it will also round your number.

var num = 5.56789;
var n = num.toFixed(2);  // n = 5.57
Jordi Castilla
  • 26,609
  • 8
  • 70
  • 109