Easy Currency Formatter In Vanilla JS – pretty-money

Category: Form , Javascript | April 8, 2021
Author:NickKaramoff
Views Total:649 views
Official Page:Go to website
Last Update:April 8, 2021
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);

Changelog:

v1.1.1 (04/08/2021)

  • Fixed invalid formatting of numbers smaller than 1

v1.1.0 (03/21/2021)

  • Updated package

You Might Be Interested In:


Leave a Reply