Description
stringmath
is a module (function) that computes the [Number]
result from the [String]
arithmetical formula.
 It does not use
eval()
 It uses regular expressions to parse [String] formulas
"2+2"
into [Number] formulas2+2
. Then it is performed as the common JavaScript arithmetic operation.
"2 + 2" //4
"3*(52)" //9
" 2.5 * 2.5 / .1" //62.5
"3.5+5*(4(3/(3+1)12*3.2*22)16/4*125/(2)+3.5+2.5*(1.52*7))16" //225.5
".25e+2*10" //250
 Any bugs found? Give me to know on GitHub
 If you need to perform arithmetic formulas with the floating point precision (avoid
0.30000000000000004
return values) or to perform arithmetic formulas with big numbers, check outexactmath
package. It implementsstringmath
features as theexactMath.formula
method.
Installation
with NodeJS / bundlers
npm install stringmath
var stringMath = require('stringmath');
stringMath("1") //1
stringMath("2/2") //1
stringMath("55") //10
with Browser
1. Add stringmath.js to the HTML file.
<head>
<script src="./stringmath.js"></script>
</head>
Any other dependencies are needed.
stringMath
global Function from stringmath.js
.
2. Use var result = stringMath("2+2");
Browser Support
Chrome  Firefox  IE  Safari  Opera 

yes  yes  yes  yes  yes 
Tests
> git clone https://github.com/devrafalko/stringmath.git
> cd stringmath
> npm install
> npm test
> npm test deep //displays error messages
Usage
stringMath(expression[,callback])
expression
[String]
 the arithmetical formula
 it can contain:

[09]
digits 
1.5
,0.5
or.5
decimal fractions 
5
,.4
,5.55
negative values 
2e2
,.25e+12
,3e10
exponential notation values 
*
multiplication sign 
/
division sign 
+
plus sign 

subtraction sign 
(
and)
parentheses

callback
[Function] (optional) (synchronous)
 by default, if the
callback
argument is omitted and theexpression
is of incorrect type or is invalid, theError
object isthrown
. If thecallback
is defined, theError
object is passed through thecallback
function, rather than beingthrown
.  if the [Function]
callback
argument is defined, it is called with the following arguments: [0]
error
It equalsnull
, if theexpression
is of correct type and is valid math formula.
Otherwise it equalsError
object.  [1]
result
It equalsnull
if theexpression
is of incorrect type or if the math formula is invalid.
Otherwise it equals [Number] result.
 [0]
Return
If the math formula is of correct type and is valid, it returns the [Number] result. Otherwise it returns null
.
Tips
 the arithmetic order of operations is respected:
 parentheses first
 then division and multiplication (from left to right)
 then addition and subtraction (from left to right)
 the multiplication sign can be omitted before parentheses;
4(2+1)
; equals to4*(2+1)
 the following signs combinations are allowed:

2 * 2
; equals to2 * (2)

2 / 2
; equals to2 / (2)

+2 + 2
; equals to2 + 2

2 + +2
; equals to 2 + 2 
2  2
equals to2 + 2

2  +2
equals to2  2

2 + 2
equals to2  2

 the (multi)spaces between values, signs and parentheses are allowed:
2 + 2
2 + ( 2  2)
2 + (+2 + +4 / 1)
.1  5
2 + 3e5
.25e+5 * .25e5
 the spaces are not allowed between:
 negative sign and value:
2   2
 period and digit in decimal fraction:
5 + . 3
 exponential notation formula:
.2 e5
,2e  5
,3e +10
 negative sign and value: