npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@greypixel_/nicenumbers

v0.0.20

Published

Sane bignumber formatting for cryptocurrencies

Downloads

86

Readme

nicenumbers

A small utility library to make displaying crpytocurrency token amounts in UIs a bit less painful.

No dependencies.

Motivation

Most web3 frontends display ERC20 token amounts. The fact that it's commonplace for ERC20s to have 18 decimals, and also that it's not uncommon for a user to hold billions of one token and tiny fractions of another means UIs have to deal with the possibility of showing numbers across a huge range.

This can be a design challenge, with the space required to token amounts varying drastically.

This library attempts to address this challenge and make our lives a little easier.

Note that this library doesn't claim to be super efficient or well written, but should only be used for presentation anyway so doesn't really need to be!

Examples

import { format } from "@greypixel_/nicenumbers";

// If using ethers, this returns a bigNumber
const tokenAmount = someErc20Contract.balanceOf(user);

// Sensible defaults, accepts ethers bigNumbers
format(tokenAmount); // 123.46k

// Specify token decimals and significant figures
format("12345678", {
  tokenDecimals: 6,
  significantFigures: 3,
}); // 12.3

// Don't use symbols (B, M, k), do use commas:
format("12345678000", {
  tokenDecimals: 6,
  significantFigures: 3,
  useSymbols: false,
  addCommas: true,
}); // 12,346

Installation

yarn add `@greypixel_/nicenumbers`

format

format is a function that accepts a string representation of a BigNumber or anything else that will either toFixed(0) or toString() to a string representation of a bignumber, and returns a nicely formatted string that can be used in the UI.

It optionally accepts an options object as outlined below.

Configuration options

All configuration options are optional, but using tokenDecimals at least is strongly recommended.

omitLeadingZero - boolean (default: false)

If set to true, will omit the leading zero for numbers less than 0, i.e. instead of 0.1 you'll get .1

tokenDecimals - number (default: 18)

When formatting an ERC20 token amount, this is the number of decimals that token has.

significantFigures - number (default: 4)

Number of significant figures to show. Will correctly round where appropriate. Note that all significant figures before the decimal point will be shown if useSymbols is set to false as the resultant number will be the same number of digits anyway in this case.

omitTrailingZeroes - boolean (default: false)

Omits trailing zeroes if the number of significant figures is more than the sig figs that exist, e.g. "1.23000" becomes "1.23"

Note: If you set useSymbols to false, this will show the full

useSymbols - boolean (default: true)

Uses symbols for large numbers, e.g. 12345 -> 12.345k. Best used with smaller numbers of significant figures.

Symbols are: B for billion (10^9), M for million (10^6), and k for thousand (10^3).

Note: addCommas has no effect if using symbols.

addCommas - boolean (default: false)

If set to true, commifies output, i.e. "12345.6789" becomes "12,345.6789"

Note: Has no effect if useSymbols is true

minimum - number | null (default: null)

Pass a number representing the minimum decimal amount to show. You should supply a decimal number, e.g. 0.01. When this is set, if the result is lower than the value provided the result will be: "<0.01".

Passing null means there's no minimum amount, and the full value will be shown. Note in this case, you can get some very long strings despite a low number of significant figures when displaying very small fractions of tokens with a large number of decimals.

Contributing

fork, branch, yarn, write code, write tests, make PR.

All PRs welcome!