Return to Snippet

Revision: 51022
at September 13, 2011 00:49 by Krummelz


Initial Code
function roundNumber(number, decimal_points) {
            if (!decimal_points) return Math.round(number);
            if (number == 0) {
                var decimals = "";
                for (var i = 0; i < decimal_points; i++) decimals += "0";
                return "0." + decimals;
            }

            var exponent = Math.pow(10, decimal_points);
            var num = Math.round((number * exponent)).toString();
            return num.slice(0, -1 * decimal_points) + "." + num.slice(-1 * decimal_points)
        }

Initial URL

                                

Initial Description
call the method like so:  onkeypress="return inputLimiter(event, 'Numbers', this.value, 5);"

Initial Title
Javascript method to round decimal values

Initial Tags

                                

Initial Language
JavaScript