# fixed-point

> Fixed point arithmetic with BigInt. Can be used for money or scientific calculations with known precision.

Latest version **0.8.0** (published 2021-10-18) · MIT license · 0 weekly downloads

## Install

```sh
npm install fixed-point
pnpm add fixed-point
yarn add fixed-point
bun add fixed-point
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.8.0 |
| Published | 2021-10-18 |
| First published | 2021-10-18 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 19.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | munrocket |
| Maintainers | munrocket |
| Keywords | fixed, money, arbitrary-precision, bigint, bigdecimal |

## Links

- npm: https://www.npmjs.com/package/fixed-point
- npm.io page: https://npm.io/package/fixed-point

## Alternatives

- [random-seedable](https://npm.io/package/random-seedable.md) — 27.9K weekly downloads
- [n2words](https://npm.io/package/n2words.md) — 22.2K weekly downloads
- [@stdlib/math-base-special-factorialln](https://npm.io/package/@stdlib/math-base-special-factorialln.md) — 5.7K weekly downloads
- [@stdlib/math-base-special-abs2](https://npm.io/package/@stdlib/math-base-special-abs2.md) — 1.7K weekly downloads
- [commons-math-interpolation](https://npm.io/package/commons-math-interpolation.md) — 1.4K weekly downloads

## Recent versions

- 0.8.0 (latest) — 2021-10-18

## README

# 🗿 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.

```js
import { FixedPoint } from 'fixed-point';

// Simple example: 0.3 - 0.2 = 0.1
console.log(new FixedPoint(0.3).sub(0.2).toString());

// Price with 30% discount, rounding benefiting the customer
FixedPoint.setRounding('floor');
console.log(new FixedPoint('99.95').mul(0.70).toFixed())

// Bitcoin fee with banker rounding: 1.0 BTC - 0.00005 BTC = 0.99995000 BTC  
FixedPoint.setRounding('even');
FixedPoint.setPrecision(8);
console.log(new FixedPoint('1').sub('0.00005').toFixed() + ' BTC');

// Rounding mode for fast calculations
FixedPoint.setRounding('trunc');
FixedPoint.setPrecision(15);
console.log(new FixedPoint('3.14159265358979').mul('2'));
```

### API

* `new FixedPoint(string)` - constructor from String
* `new FixedPoint(number)` - constructor from Number
* `a.toString()` - conversion to String
* `a.toFixed()` - conversion to String
* `FixedPoint.setPrecision(number)` - set precision (2 decimal places is default)
* `FixedPoint.setRounding(string)` - set rounding ('trunc'|'floor'|'ceil'|'even'), 'even' is default
* `a.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
- [x] constructors, toString/toFixed
- [x] operations: +|-|*|/
- [x] 4 rounding modes from IEEE 754-1985: RN, RZ, RU, RD
- [x] testing rounding modes with table
- [x] get feedback
- [ ] add benchmark
- [ ] add precision conversion: x.xxxxxxxx BTC in xx.xx $
- [ ] add build with BigInt polyfill

---
_Source: https://npm.io/package/fixed-point · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
