UNPKG

@js-draw/math

Version:
27 lines (26 loc) 788 B
import { numberRegex } from './constants.mjs'; /** * Returns the length of `numberAsString` after a decimal point. * * For example, * ```ts * getLenAfterDecimal('1.001') // -> 3 * ``` */ export const getLenAfterDecimal = (numberAsString) => { const numberMatch = numberRegex.exec(numberAsString); if (!numberMatch) { // If not a match, either the number is exponential notation (or is something // like NaN or Infinity) if (numberAsString.search(/[eE]/) !== -1 || /^[a-zA-Z]+$/.exec(numberAsString)) { return -1; // Or it has no decimal point } else { return 0; } } const afterDecimalLen = numberMatch[3].length; return afterDecimalLen; }; export default getLenAfterDecimal;