Question

function calc() {
 ppdObj = document.getElementById("ppd");
 ppdV = ppdObj.value;
 pppObj = document.getElementById("ppp");
 pppV = pppObj.value;
 ppcObj = document.getElementById("ppc");
 ppcV = ppcObj.value;
 oceObj = document.getElementById("oce");
 oceV = oceObj.value;
 savePerDay = ppdV * (pppV - ppcV / oceV).toFixed(0);
 savePerMonth = 30 * ppdV * (pppV - ppcV / oceV).toFixed(0);
 savePerYear = 365 * ppdV * (pppV - ppcV / oceV).toFixed(0);
 txt = "<br>You would save £"+savePerYear+" per Year!";
 txt += "<br>And you would save £"+savePerMonth+" per Month!<br>";
 saveObj = document.getElementById("saveDiv");
 saveObj.innerHTML = txt;

I am just at the early stages of learning javascript. Please could you advise me how I can make this calculator display no decimal points. At the moment the results vary, from none to 1 to 10 decimal points? Thank you very much for your help.

Was it helpful?

Solution 2

You can use Math.floor(savePerYear) to discard the decimals:

var a = 1;
var b = 1.5;
var c = a + b;
var rounded = Math.floor(c);
console.log(rounded); // outputs: 2

OTHER TIPS

You can also use toFixed to set your output to a certain number of decimal points.

var x = 2.4926492
x.toFixed(2);    // output is 2.49
x.toFixed(0);    // output is 2

Hope this helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top