JavaScript Number Formatting

I don't know why this function is not more well known or why it never comes up when I do a search for "javascript round method", but I have always had a terrible time coming up with a reliable floating-point rounding function for JavaScript that holds a definite number of places. Usually the search comes back with somebody's custom method for rounding to a specific number of digits that has flaws of some sort. The built-in toFixed() method fixes all of this. It is a built in method of all JavaScript Numeric types. For example:

Var myFloat = 0.12345;
Alert(myFloat.toFixed(4)); // displays 0.1235

myFloat = 1.23;
alert(myFloat.toFixed(4)); // displays 1.2300

Comments