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:
You can read more about the available configuration parameters in the next section, Config.
Config
currency
Type:string Default:""
The string to be used as currency symbol.
It can be a respective sign (like "quot;), currency code (like "GBP") or a word (like "peso").
decimalDelimiter
Type:string Default:"."
The string that separates the integer and the fractional parts of the number.
maxDecimal
Type:number Default:2
The maximum number of decimal places allowed in the number.
minDecimal
Type:number Default:0
The minimum number of decimal places allowed in the number. Has no effect when decimals is set to "fixed".
decimals
Type:string Values:"fixed", "fluid" or "minmax" Default:"minmax"
Sets the strategy to calculate the amount of decimal places.
"fixed" — the amount of places will always stay at maxDecimal. minDecimal has no effect.
"fluid" — the amount of places will stay at any number between minDecimal and maxDecimal, in order not to have trailing zeros.
"minmax" — the amount of places will stay at maxDecimal unless it's possible to be at minDecimal without having trailing zeros.
position
Type:string Values:"before" or "after" Default:"after"
Sets the position of the currency symbol with respect to the number.
spaced
Type:boolean Default:true
Sets whether there should be a space between the number and the currency symbol.
thousandsDelimiter
Type:string Default:""
A string that separates the thousands of the number.
Difference from toLocaleString
ECMAScript's 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 a lot of built-in locales. However, the output of it is different on different Node.js versions and browsers:
let price = (10000).toLocaleString("ru", {
style: "currency",
currency: "RUB"
});
console.log(price);
//=> "10 000,00 ₽" in modern browsers
//=> "RUB 10,000.00" in Node v12.13.0
//=> "RUB 10,000" in Node v4.8.6
This can lead to unexpected output and difficulties in debugging.
While pretty-money doesn't have any locales built-in, it provides a flexible API, so that the end user can compose any currency formatting function they need.
let price = prettyMoney({
currency: "₽",
thousandsDelimiter: " "
}, 10000);
console.log(price);
//=> "10 000 ₽" in every Node, in every browser
Development
If you want to improve pretty-money, create your own fork of it or just play around with the developer build, here's all you need to know:
yarn dev to start a dev server, which will automatically build the library after you change the source and output it to ./dist/
yarn build to build the production-ready minified version of the library and output it to ./dist/
yarn test to build the project and run all tests, which include:
yarn test:lint to check the code formatting with ESLint (this won't auto fix errors)
yarn test:unit to run the uvu unit tests and calculate coverage
yarn test:size to check the size
There are no peer dependencies and other extra requirements. Any help is welcome when it keeps things simple and small.