
Company News
Socket Named Top Sales Organization by RepVue
Socket won two 2026 Reppy Awards from RepVue, ranking in the top 5% of all sales orgs. AE Alexandra Lister shares what it's like to grow a sales career here.
decimal-eval
Advanced tools
A small, safe, fast JavaScript library for parsing decimal arithmetic.
# use npm
npm i -S decimal-eval
# or use yarn
yarn add decimal-eval
Support the four arithmetic operations of addition, subtraction, multiplication and division, and automatically fix JS decimal precision by big.js.
import {evaluate} from 'decimal-eval';
evaluate('0.1 + 0.2') // 0.3
evaluate('100 * (0.08 - 0.01)'); // 7
evaluate('1 + abc', { abc: 2 }); // 3
In addition to the above operators, it also supports custom operator expansion, and supports unary operators and binary operators. The operator precedence according to: MDN operator precedence.
import {evaluate, Parser, Operator} from 'decimal-eval';
// create binary operator `add`, the precedence is 13
const addOp = Operator.create('add', 13, (left, right) => {
return left + right;
});
// create unary operator `sin`, the precedence is 16
const sinOp = Operator.create('sin', 16, (value) => {
return Math.sin(value);
}, true);
// install custom operators
Parser.useOperator(addOp);
Parser.useOperator(sinOp);
// same as: `1 + Math.sin(-2)`
evaluate('1 add sin -2') // 0.09070257317431829
evaluate(expression: string, scope?: Record<string, number>): numberParse and calculate arithmetic expression.
import {evaluate} from 'decimal-eval';
evaluate('1 + 2'); // 3
evaluate('1 + abc', { abc: 2 }); // 3
Operator.create(value: string, precedence: number, calc: Function, isPrefix = false)import {Operator} from 'decimal-eval';
// create operator `%`, which is a binary operator, the calc should like: `(left: number, right: number) => number`
const modOp = Operator.create('%', 15, (left, right) => left % right);
// `isPrefix` is true, that is a unary operator, the calc should like: `(value: number) => number`
const absOp = Operator.create('abs', 16, (value) => Math.abs(value), true);
new Parser(expression: string).parse(): ASTParse arithmetic expressions.
import {Parser} from 'decimal-eval';
const ast = new Parser('1 + 2').parse();
new Parser(expression: string).compile(): (scope) => numberCompile and cache expression.
import {Parser} from 'decimal-eval';
const evaluate = new Parser('1 + abc');
evaluate({ abc: 2 }); // 3
evaluate({ abc: 9 }); // 10
evaluate({ def: 1 }); // throw error
Parser.useOperator(operator)Install an operator, which created by the Operator.create() method.
Parser.useAdapter(adapter)Custom setting calculation adapter method to four arithmetic (+, -, *, /).
Big.js is used by default.
Parser.useAdapter({
'+': (left, right) => left + right,
'-': (left, right) => left - right,
'*': (left, right) => left * right,
'/': (left, right) => left / right
})
Parser.evaluate(expression: string): numberAlias of evaluate(expression: string) method.
FAQs
A tiny, safe, fast JavaScript library for decimal arithmetic expressions.
The npm package decimal-eval receives a total of 1,474 weekly downloads. As such, decimal-eval popularity was classified as popular.
We found that decimal-eval demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Company News
Socket won two 2026 Reppy Awards from RepVue, ranking in the top 5% of all sales orgs. AE Alexandra Lister shares what it's like to grow a sales career here.

Security News
NIST will stop enriching most CVEs under a new risk-based model, narrowing the NVD's scope as vulnerability submissions continue to surge.

Company News
/Security News
Socket is an initial recipient of OpenAI's Cybersecurity Grant Program, which commits $10M in API credits to defenders securing open source software.