Javascript Math Reference

Javascript Math.round


It is often desirable to remove the decimal part of a number. The Math.round function exactly does that.
To cosider the usage, consider calculating the cumulative value of a bank deposit using the formula

M = $1000 x ( 1 + R/100) n

If you use the formula and try to print it, it give you results that spans several digits, beyond the decimal point. Math.round helps here. Take a look at the example below - you can try it online here

<html>
<body>
<script type="text/javascript">
<!--

/*
********************************************************
Example - Usage of Math.round
********************************************************
*/
var initial_value = 1000;
var rate = 6;
var number_of_years = 5; 

var cumulative = initial_value * Math.pow( (1+rate/100), number_of_years); 

document.write ("Cumulative value is : " + cumulative);

document.write ("<br /> Cumulative value rounded is : " + Math.round(cumulative));

 //-->
</script>
</body>
</html>


Math.pow takes two arguments and returns one value value. You may like to try this program online here . Make changes in the program and see its results.