Simplest way of getting the number of decimals in a number in JavaScript [duplicate]
Number.prototype.countDecimals = function () { if(Math.floor(this.valueOf()) === this.valueOf()) return 0; return this.toString().split(“.”)[1].length || 0; } When bound to the prototype, this allows you to get the decimal count (countDecimals();) directly from a number variable. E.G. var x = 23.453453453; x.countDecimals(); // 9 It works by converting the number to a string, splitting at the . … Read more