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

number-currency-format

v1.0.10

Published

Dependency-less price formatting

Downloads

5,995

Readme

npm version Build Status

Number currency format

Zero-dependency, cross-browser, lightweight and flexible utility for number/price formatting that you control.

Installation

npm i number-currency-format

or

yarn add number-currency-format

Usage

Format

const { format } = require('number-currency-format');

format(1999.99);
// '1,999.99'

Setting currency

format(1999.99, {
    currency: '€'
});
// '1,999.99 €'

Setting other formatting options

format(1999.99, {
    currency: '$',
    spacing: false,
    currencyPosition: 'LEFT'
})
//'$1,999.99'

Unformat

const { unformat } = require('number-currency-format');

unformat('1.999,99');
// 1999.99

Currencies

const { unformat } = require('number-currency-format');

unformat('$ 199.990,05');
// 199990.05

To see all supported usecases and example usages, run npm test or yarn test.

API

format

format(number: number, options?: FormattingOptions)

Formats number given defined formatting options

  • number: number - Number to be formatted
  • options?: FormattingOptions - Formatting options (optional)

FormattingOptions

  • currency: string - Currency symbol to be printed next to the formatted number. By default: none
  • thousandSeparator: string - Symbol separating thousands. By default: ,
  • decimalSeparator: string - Symbol separating decimals. By default: .
  • decimalsDigits: number - Number of decimal digits. By default: 2
  • showDecimals: string - ALWAYS, IF_NEEDED, AS_IS or NEVER. IF_NEEDED does not show the decimal if it is 0 (and if it is different than 0, shows exactly {decimalsDigits} decimal digits). AS_IS preserves the decimal depth of the source number. By default: ALWAYS
  • currencyPosition: string - LEFT or RIGHT. By default: RIGHT
  • spacing: boolean - Spacing between currency and price. By default: true
  • arithmeticalRounding: boolean - Use arithmetical rounding (always half-up) instead of tie break rounding. By default: false (so: the rounding will include tie-breaking)

All configuration is optional.

unformat

unformat(text: string, options?: FormattingOptions)

Reads the number out of a string that looks like a price.

  • text: text - Text to be unformatted
  • options?: FormattingOptions - Formatting options (optional). Currently only decimalSeparator is supported by the unformatter as the rest of the options don't really make sense and the unformatter can extract the price out of the text anyway.

Compatibility

This works in every modern browser as well as on the server. This module does not contain and will never contain any dependency at all. This module does not use any 3rd party bundler, transpiler, compiler or test runner. Vanilla all the way 😎.

Why not using intl or other packages?

Intl does not offer full flexibility around price formatting and has an overhead of loading all locale files. Additionally it requires polyfill for old browsers and headless testing. This library is bare to the bone and does not contain all possible locales definition and gives you all possible formatting options to support all known price formats.