Skip to content

Instantly share code, notes, and snippets.

@robboerman
robboerman / gist:34f8a7f6dd14b735a107
Last active April 2, 2023 18:36
Check float precision of a number in JavaScript
/**
* Checks the float precision of a number in JavaScript wether it's a number or string
* @param {Number}/{String} number to be checked, can be string representation
* @param {Number} precision, max amount of characters not counting minus and .
* @param {Number} decimals, max amount of decimal characters
* @return {Boolean} value satisfies precision and decimals
*
* Example:
* - checkPrecision(12.3456, 6, 2) -> false, too many decimals
* - checkPrecision(-123456.7, 6, 2) -> false, too many total characters