0.2.4 • Published 3 years ago

eval-num-expr v0.2.4

Weekly downloads
-
License
MIT
Repository
github
Last release
3 years ago

Evaluate Numerical Expressions

Installation

npm install --save eval-num-expr

Usage

import evalExpr from "eval-num-expr";

const value = evalExpr("100 + 3 * 55 + 73 - 73 * 18%");
// value = 324.86

// With Invalid Expressions handling
try {
  const value = evalExpr("100 + (3 * 55");
} catch (e) {
  // Will throw an exception for invalid expressions
  console.error(e.message);
}

You can use basic operations (e.g. +, -, *, /, %, ^ etc.) in you expressions.

Examples

  • 175 + 40 + 18% = 253.7
  • 10 + (100 - 100 * 18%) * 5 + 40 / 20 = 422

NOTE: Floating points must be handled carefully. Use Number(<number>).toFixed(<digits>) to fix the digits after decimal point.

Api

This package supports TypeScript.

evalExpr

This is the default and only export from this package.

/**
 * Evaluate numeric mathematical expression
 * @param string expression - The expression to evaluate ("24 + 65 * 3 - 48")
 * @return number - The evaluated value.
 *
 * @throws Will throw an error if the expression in invalid.
 *
 * NOTE: When passing floating points, please handle the digits after decimal point
 * e.g. 2 - 1.1 leads to 0.8999999999999999 intead of .9
 */
evalExpr(expressions: string)
0.2.3

3 years ago

0.2.2

3 years ago

0.2.4

3 years ago

0.2.1

3 years ago

0.2.0

3 years ago

0.1.3

3 years ago

0.1.2

3 years ago

0.1.1

3 years ago

0.1.0

3 years ago

0.0.3

3 years ago

0.0.2

3 years ago