Posted By

Krummelz on 09/13/11


Tagged

decimal round values


Versions (?)

Javascript method to round decimal values


 / Published in: JavaScript
 

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

  1. function roundNumber(number, decimal_points) {
  2. if (!decimal_points) return Math.round(number);
  3. if (number == 0) {
  4. var decimals = "";
  5. for (var i = 0; i < decimal_points; i++) decimals += "0";
  6. return "0." + decimals;
  7. }
  8.  
  9. var exponent = Math.pow(10, decimal_points);
  10. var num = Math.round((number * exponent)).toString();
  11. return num.slice(0, -1 * decimal_points) + "." + num.slice(-1 * decimal_points)
  12. }

Report this snippet  

You need to login to post a comment.