Money With Wings emoji

pretty-money

A tiny currency formatting library for JavaScript

What is it?

pretty-money is a currency formatting library for JavaScript.

It can help you format financial values in any way you desire. While being highly customizable, pretty-money is easy to use and extremely lightweight (468 bytes minzipped).

pretty-money is dependency-free and totally not biased towards browser or Node versions. Unlike Number.prototype.toLocaleString, pretty-money produces the same code in every environment.

Demo

Formatted: $ 1,000

Config

Currency symbol

Position

Spaced

Decimal places

Mode

Delimiters

Install

pretty-money can be installed as an npm module:

npm install pretty-money
# or
yarn add pretty-money

...or served from CDN like jsDelivr:

<script
  src="https://cdn.jsdelivr.net/npm/pretty-money@1.0/dist/pretty-money.umd.js"
>
</script>

Usage

There are two ways to use pretty-money: traditional and functional. Traditional way is to call the function with two parameters: the config object and the number you need to format:

const prettyDollarConfig = {
    currency: "$",
    position: "before",
    spaced: false,
    thousandsDelimiter: ","
}

const priceA = prettyMoney(prettyDollarConfig, 1234); //=> "$1,234"
const priceB = prettyMoney(prettyDollarConfig, 567.89); //=> "$567.89"

Functional way is to curry the function, i.e. to create a function with a set config and to later call it with only one parameter — the number to format:

const prettyEuro = prettyMoney({
    currency: "€",
    decimals: "fixed",
    decimalDelimiter: ",",
    thousandsDelimiter: "."
})

const priceA = prettyEuro(1234); //=> "1.234,00 €"
const priceB = prettyEuro(567.89); //=> "567,89 €"

You can read more about how to configure pretty-money in the README.