Easy Currency Formatter In Vanilla JS – pretty-money

Category: Form , Javascript | February 5, 2020
Author: NickKaramoff
Views Total: 91
Official Page: Go to website
Last Update: February 5, 2020
License: MIT

Preview:

Easy Currency Formatter In Vanilla JS – pretty-money

Description:

pretty-money is a lightweight and convenient currency formatter used to format and prettify currency (money) and numbers with custom symbol, delimiters, and decimal places.

How to use it:

1. Install the package and import the pretty-money as a module.

# NPM
$ npm install pretty-money --save
import prettyMoney from "pretty-money";

2. Or directly load the umd version of the library from a CDN.

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

3. Format a numerical value you provide:

let price = prettyMoney({ 
    currency: "$" 
}, 10000); 

//=> "10000 $"

4. Set the position of the currency symbol.

let price = prettyMoney({ 
    currency: "$",
    position: "before"
}, 10000); 

//=> "$ 10000"

5. Customize 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”: default
let price = prettyMoney({ 
    currency: "$",
    position: "before",
    decimals: "fixed"
}, 10000);

6. Customize the decimal and thousands delimiters.

let price = prettyMoney({ 
    currency: "$",
    position: "before",
    decimals: "fixed",
    decimalDelimiter: ",",
    thousandsDelimiter: "."
}, 10000); 

//=> "$ 10.000,00"

7. Set the min/max number of decimal places.

let price = prettyMoney({ 
    maxDecimal: 2,
    minDecimal: 0
}, 10000);

You Might Be Interested In:


Leave a Reply