Created
August 29, 2013 09:19
-
-
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);"
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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