Giter VIP home page Giter VIP logo

fpnum's Introduction

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);

fpnum's People

Contributors

tomquirk avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.