Today I would like to discuss a handy function when we are working with decimal numbers…toFixed. What this does is truncate a decimal number to whatever number of spaces we pass to the toFixed method. So if we are working with money, for example, we can truncate a calculation to two decimal places…toFixed(2). Here is an example…
[code lang=”js”]
// Create a variable to house a decimal
var myNum = 10.123;
// Display the number truncated
document.write(myNum.toFixed(2));
[/code]
The aforementioned code would display as follows…
Note: this method does traditional rounding…five (5) or greater will round up.
Happy Coding!
Clay Hess