1.0.0 • Published 2 months ago

fpnum v1.0.0

Weekly downloads
-
License
ISC
Repository
-
Last release
2 months ago

fpnum

A tiny lib for working with fixed-point numbers in JS. For Ethereum dApps, financial applications, whatever.

Getting Started

Installation

npm install fpnum

Usage

FixedInt

Basic conversion to and from a bigint's fixed-point representation. Similar to viem's parseUnits, formatEther etc.

// initialize from a bigint with 18 decimals
const oneEther = new FixedInt<18>(1_000_000_000_000_000_000, 18);
const oneGwei: FixedInt<9> = oneEther; // type error! An 18-decimal FixedInt can't be assigned to a 9 decimal one

// initialize from the formatted representation
// "1.1" => FixedInt(1_100_000_000_000_000_000, 18)
const onePointOneEther: FixedInt<18> = FixedInt.parse<18>("1.1", 18);

// 1_100_000_000_000_000_000n: bignumber
console.log(onePointOneEther.val);

// "1.1": string
console.log(onePointOneEther.format());

// 1.1: number
console.log(onePointOneEther.toFloat());

// create a new class for easier reuse
export class Ether extends FixedInt<18> {
  constructor(value: bigint) {
    super(value, 18);
  }
}
// "2" => FixedInt(2_000_000_000_000_000_000, 18)
const twoEther = Ether.parse("2");

FixedPortion

Useful for when a fixed-point number represents a portion of some maximum value. For example: a fee percentage, interest rates, etc.

const MAXIMUM_FEE = 10_000n; // 100% as a fixed-point number with 4 decimals
// initialize from a bigint with 18 decimals
const onePercent = new FixedPortion<4>(100, 4, MAXIMUM_FEE);

// initialize from the formatted representation
// "0.2" => FixedInt(200, 4)
const fee = FixedPortion.parse<4>(".2", 4, MAXIMUM_FEE);

// 2: number
console.log(fee.formatPercentage());

// update fee from 2% to 3% with the percentage representation (a fixed-point number with 2 decimals)
fee.setPercentage(3);
1.0.0

2 months ago

0.0.17

2 months ago

0.0.16

5 months ago

0.0.14

5 months ago

0.0.15

5 months ago

0.0.13

8 months ago

0.0.12

8 months ago

0.0.11

8 months ago

0.0.10

8 months ago

0.0.9

8 months ago

0.0.8

8 months ago

0.0.7

8 months ago

0.0.5

8 months ago

0.0.4

8 months ago

0.0.3

8 months ago

0.0.2

8 months ago

0.0.1

8 months ago