#Form Building - Counting Decimal Places
Use the following Page-Level JavaScript function to count the number of decimal places in a given field. The function will work with either Number elements or Text elements. If the supplied value cannot be treated as a number then the function will return false.
/** * Returns the number of decimals in a numeric value * @param {string or numeric} n value can be numeric data type or string * @return {int} number of digits after decimal, false if input is NaN */ function numDecimals(n) { if(typeof n === "undefined" || n == null || isNaN(n) === true) return false; var input_value = String(n); if(input_value.indexOf(".") === -1) { return 0; } else { return input_value.split(".")[1].length; } }
Please sign in to leave a comment.
Comments
0 comments