Parses a string as a mathematical expression.
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
dist
spec
src
test
.babelrc
.eslintrc
.gitattributes
.gitignore
.jsdocrc
CHANGELOG.md
LICENSE.md
README.md
package-lock.json
package.json

README.md

math-from-string

Parses a string as a mathematical expression. Supports addition, subtraction, division, multiplication, and exponentiation.

Usage

Node

npm i math-from-string

let mathfromString = require("math-from-string");

Minified version:

let mathfromString = require("math-from-string/dist/math-from-string.min");

Browser

import "math-from-string";

Minified version:

import "math-from-string/dist/math-from-string.min";

Script tag:

<script src="node_modules/math-from-string/dist/math-from-string.min.js"></script>

Notes

  • This project is lightweight and dependency free.
  • The source code is written in ES6 and transpiled with Babel.

Samples

mathfromString(str);

mathfromString("2+2");
// 4

mathfromString("(5-3)*2");
// 4

mathfromString("5-3*2");
// -1

mathfromString("-10+-2");
// -12

mathfromString("2**3");
// 8

mathfromString("(2+8)*2+(100.34-10+(2+5.4))");
// 117.74000000000001

See the documentation.

See the CHANGELOG.

License

Copyright (c) 2014, 2017-2018 Leandro Silva (http://grafluxe.com)

Released under the MIT License.

See LICENSE.md for entire terms.