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

decimal-fns

v1.1.0

Published

Useful functions to make working with Decimal.js more enjoyable

Downloads

419

Readme

decimal-fns

Useful functions to make working with Decimal.js more enjoyable.

Installation

npm i decimal-fns decimal.js

NOTE: This package relies on Decimal.js as a Peer Dependency and does not bundle it as part of this repository.

Usage

import { asDecimal, formatNumber, humanNumber, isDecimal} from 'decimal-fns'

const number = 5
const decimal = asDecimal(5)

const biggerDecimal = decimal.plus("2000") // 2005

isDecimal(biggerDecimal) // true

formatNumber(biggerDecimal) // "2,005"

humanNumber(biggerDecimal.times(100)) // "200.5K"

Exported Methods

asDecimal(value, [fallback])

Converts a Decimal-Like param (string, number, Decimal) and attempts to convert it to the Decimal type. If the conversion fails (e.g. NaN is provided), it will attempt to return the fallback parameter as a Decimal. If no fallback is provided, or if the fallback can not be converted to a Decimal (e.g. NaN is provided), then the method will throw.

import { asDecimal } from 'decimal-fns'

const value = "5"
const decimal = asDecimal(value, 0) // returns a Decimal of 5
const fallback = asDecimal(undefined, 0) // returns a Decimal of 0

isDecimal(value)

Determines if a parameter is a Decimal type, while also indicating to the Typescript parser to treat the item as a Decimal going forward.

import { isDecimal } from 'decimal-fns'

isDecimal(5) // false
isDecimal(new Decimal(5)) // true

formatNumber(value, { numDecimals })

Format a number with thousands separators and a fixed number of decimal places.

import { formatNumber } from 'decimal-fns'

formatNumber(5) // "5"
formatNumber(5000.555555, { numDecimals: 1 }) // "5,000.6"
formatNumber(1234567890) // "1,234,567,890"

humanNumber(value, { useWords })

Convert a Decimal-Like param (string, number, Decimal) to a human-readable number in a string.

import { humanNumber } from 'decimal-fns'

humanNumber(5) // "5"
humanNumber(new Decimal(5000)) // "5K"
humanNumber(1234, { useWords: true }) // "1.23 thousand"
humanNumber("1234560") // "1.2M"
humanNumber(1234567890) // "1.23B"
humanNumber(1_234_560_000_000) // "1.235T"

Brought to you by M2 Labs

This project is maintained and funded by M2 Labs, a Web3 product development studio.