@pinkchen/calc
v1.0.9
Published
The ultimate solution for precise front-end calculations, featuring accurate computation, lightweight design, convenience, maintainable calculation formulas, integrated decimal.js mathematical functions, and support for custom functions.
Downloads
269
Maintainers
Readme
@pinkchen/calc
The ultimate solution for precise front-end calculations, featuring accurate computation, lightweight design, convenience, maintainable calculation formulas, integrated decimal.js mathematical functions, and support for custom functions.
Advantages
- 💪 Supports precise calculations
- 🎈 Lightweight with small bundle size
- 💼 Convenient, supports es, cjs, umd
- 🔢 Integrates decimal.js internal mathematical functions
- ⚒️ Supports custom functions to meet all customization needs
- 👨🏻🔬 Support scientific notation
- 🌰 Successfully pass 500 test cases
Documentation
English|中文文档
Installation
npm i @pinkchen/calc -S
// or
yarn add @pinkchen/calc
// or
pnpm add @pinkchen/calc
// or
bun add @pinkchen/calcImport
- cjs
const { calc } = require('@pinkchen/calc');- es
import { calc } from '@pinkchen/calc';- umd
<script src="node_modules/@pinkchen/calc/dist/umd/index.js"></script>
<script>
window.pinkchen.calc('0.1+0.2'); //0.3
// Refer to specific usage examples for other usages
</script>Usage
Basic Calculation
calc('0.1+0.2'); //0.3
calc('22.22*22.22'); //493.7284
calc('(22.22*22.22)'); //493.7284
calc('1 + 2 * (3 - 4 * (5 + 6))'); //-81Calculation with Data Source
calc('1+22.22/(22.22+a)', { a: 22.22 }); //1.5
calc('a*b', { a: 11, b: 22 }); //242
calc('1+a*b', { a: 11, b: 22 }); //243
calc('aa*bb.bb', { aa: 22, b: 22, bb: { bb: 33 } }); //726
calc('oo.a+oo.b*(oo.c-oo.d.d*(oo.e.e+oo.ff))', {
oo: { a: 1, b: 2, c: 3, d: { d: 4 }, e: { e: 5 }, ff: 6 }
}); //-81Calculation with Functions
It integrates static mathematical functions from decimal.js. Refer to decimal.js for available functions.
Built-in functions must be used with the prefix "DC."; for example: DC.max(1, 2)
calc('DC.max(a+b,0.3)', { a: 0.1, b: 0.2 }); //0.3
calc('1+DC.max(a+b,0.3)', { a: 0.1, b: 0.2 }); //1.3
calc('1+DC.abs(-1-a-1)', { a: -1 }); //2Support custom Function
You can achieve customization by injecting custom functions into the data source.
calc('a+max(getSum(a,b)+1, a+c, a+111/(d*e))', {
a: 11,
b: 12,
c: 133,
d: 14,
e: 15,
getSum: (a, b) => a + b
}); //155
calc('a+getSum(max(a*b, a+b), b)', { a: 1, b: 2, getSum: (a, b) => a + b }); //6
calc('a.a*(b + c + getOne())', { a: { a: 2 }, b: 3, c: 3, getOne: (b) => 1 }); //14❗❗❗ Note: Whether built-in or custom functions, all parameters will be converted using Decimal, so please handle parameters as numbers inside custom function bodies.
Formatting
calc('2222.22*2222.22', {}, { separator: true }); //4,938,261.7284
calc('2222.22*2222.22', {}, { digit: 6 }); //4938261.728400
calc('2222.22*2222.22', {}, { preUnit: '$ ' }); //$ 4938261.7284
calc('2222.22*2222.22', {}, { postUnit: ' USD' }); //4938261.7284 USD
calc('2222.22*2222.22', {}, { percentage: true }); //493826172.84%
calc('2222.22*2222.22', {}, { permillage: true }); //4938261728.4‰
calc(
'99.9949999/100',
{},
{
separator: true,
digit: 2,
preUnit: 'Your skill level exceeds ',
postUnit: ' of peers',
percentage: true
}
); //Your skill level exceeds 99.99% of peersSupport scientific notation
calc('DC.abs(-1e2,5e1,-2e1)'); //100
calc('9.99e15 * 9.99e15'); //9.98001e31
calc('1e0 / (1e0 + DC.exp((1.5e1 - 1.2e1) / (2.585e-2 * 3e2)))', {}, { digit: 6 }); //0.404477