What does toFixed(2) mean in javascript?

goldy300

New Member
Messages
33
Reaction score
0
Points
0
I pretty much understand everything in this script but wasn't sure about what toFixed(2) ment???

let me know!

var distance = parseFloat(document.form1.distance.value);
var fuel = parseFloat(document.form1.fuel.value);
var cost = parseFloat(document.form1.cost.value);

var totalcost = fuel * cost;
var kmperl = distance / fuel;
var perhunge = (fuel / distance) * 100;
var mperg = (distance * 0.6214) / (fuel * 0.2642);
document.write("<center><h2>And the winner is...</h2>");
document.write("The total cost of the trip was <b>$" + totalcost.toFixed(2) + "</b><br>The total kilometres travelled per litre was <b>" + kmperl.toFixed(2) + "kms</b><br>The total litres consumed per 100km's was <b>" + perhunge.toFixed(2) + " litres</b><br>The miles per gallon were <b>" + mperg.toFixed(2) + " miles/gallon</b>.</center>");

Thanks.
Edit:
Never mind...

The toFixed() method rounds the Number to the specified number of decimal places.
 
Last edited:
Top