Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Krummelz/6375969 to your computer and use it in GitHub Desktop.
Save Krummelz/6375969 to your computer and use it in GitHub Desktop.
Call the method like so: onkeypress="return inputLimiter(event, 'Numbers', this.value, 5);"
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);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment