🗿 fixed-point
Fixed point arithmetic with BigInt. Minimalistic library that can be used for money or scientific calculations with known precision.
Examples
Install package npm install fixed-point
or include minified script on page.
import { FixedPoint } from 'fixed-point';
console.log(new FixedPoint(0.3).sub(0.2).toString());
FixedPoint.setRounding('floor');
console.log(new FixedPoint('99.95').mul(0.70).toFixed())
FixedPoint.setRounding('even');
FixedPoint.setPrecision(8);
console.log(new FixedPoint('1').sub('0.00005').toFixed() + ' BTC');
FixedPoint.setRounding('trunc');
FixedPoint.setPrecision(15);
console.log(new FixedPoint('3.14159265358979').mul('2'));
API
new FixedPoint(string)
- constructor from Stringnew FixedPoint(number)
- constructor from Numbera.toString()
- conversion to Stringa.toFixed()
- conversion to StringFixedPoint.setPrecision(number)
- set precision (2 decimal places is default)FixedPoint.setRounding(string)
- set rounding ('trunc'|'floor'|'ceil'|'even'), 'even' is defaulta.add(b)
- addition (exact)a.sub(b)
- subtraction (exact)a.mul(b)
- multiplication (rounded)a.div(b)
- division (rounded)a.eq(b)
- a == b
a.ne(b)
- a != b
a.gt(b)
- a > b
a.ge(b)
- a >= b
a.lt(b)
- a < b
a.le(b)
- a <= b
Status: alpha.
2do