- Small. Dependency-free. 468 bytes minified and gzipped. Controlled by Size Limit.
- Functional. The function can be used traditionally or curried for later reuse.
- Flexible. The config can be tweaked to present any modern currency.
;let price = ; //=> "10000 EUR"
Works everywhere where there is support for ES3.
Difference from toLocaleString
Number has a method
toLocaleString, which has a similar idea. It too can be used to format numbers as
financial values and it even has built-in locales, however the output of the function is different on different Node
versions and browsers.
let price = 10000;console; //=> "10 000 ₽" on Node < 12.somethingconsole; //=> "10,000.00 RUB" on Node >= 12.something
While pretty-money doesn't have any locales built-in, it provides a better API, so that the end user can compose any currency formatting function they need.
let price =10000;console; //=> "10 000 ₽" on any Node, in any browser
pretty-money is available on NPM, so you can install it your usual way:
npm install pretty-money// oryarn add pretty-money
If you want to use pretty-money in browser, you can install the latest version with jsDelivr:
There are two ways you can use a formatting function: traditional and functional.
Traditional way is to extract a config object for reuse and to call the function with two parameters — config and number — every time:
const prettyDollarConfig =currency: "$"position: "before"spaced: falsethousandsDelimiter: ","const priceA = ; //=> "$1,234"const priceB = ; //=> "$567.89"
Functional way is to curry the function, i.e. to create a function with set config and to call it with one parameter — number:
const prettyEuro =const priceA = ; //=> "1.234,00 €"const priceB = ; //=> "567,89 €"
You can read more about how to configure pretty-money in the next section, Config.
The string to be used as currency symbol. It can be the respective sign (like "$"), currency code (like "GBP") or a word (like "peso").
A string that separates the integer and the fraction parts of the number.
Sets the amount of decimal places.
"fixed"— the amount of places will always stay at
minDecimalhas no effect.
"fluid"— the amount of places will stay at any number between
maxDecimal, in order not to have trailing zeros.
"minmax"— the amount of places will stay at
maxDecimalunless it's possible to be at
minDecimalwithout having trailing zeros.
The maximum number of decimal places allowed in the number.
The minimum number of decimal places allowed in the number. Has no effect when
Sets the position of the currency symbol with respect to the number.
Sets whether there should be a space between the number and the currency symbol.
A string that separates the thousands of the number.