pols-utils - v5.3.11
    Preparing search index...

    Function formula

    • Evaluates a mathematical expression given as a string. Supports the use of parameter placeholders (prefixed with $), which are replaced with values from the parameters object before evaluation.

      You can use numbers, placeholders, and the following functions within the expression:

      • avg(p1, p2, ...): Returns the average of the given numbers.
      • pow(p1, p2): Returns the result of raising p1 to the power of p2.
      • pi(): Returns the value of PI.
      • e(): Returns the value of Euler's number (e).

      Parameters

      • toEval: string

        The formula string to evaluate. Placeholders must be in the form of $paramName.

      • Optionalparameters: PFormulaParameters

        An optional object containing values to replace placeholders in the formula.

      Returns { formula: string; result: number }

      The results of the evaluated formula.

      console.log(PUtilsMath.formula('1 + 2')) // 3
      console.log(PUtilsMath.formula('5 * 6')) // 30
      console.log(PUtilsMath.formula('$one + 10', { one: 3 })) // 13
      console.log(PUtilsMath.formula('$one + $two', { one: 5, two: '$one' })) // 10
      console.log(PUtilsMath.formula('pi() + 1')) // 4.1416...