A lightweight library used to format a number to a currency format and parse such values back to numbers.
npm install @kainiedziela/monetize --save
import { monetize, demonetize } from "@kainiedziela/monetize"
monetize(1.23) // -> "$1.23"
demonetize("$1.23") // -> 1.23
When using demonetize
on a monetized value that had custom options remember to pass those same options to as a param to demonetize
, otherwise it may not parse such a value properly. This mainly concerns decimal
and negativePattern
options.
You can customize the formatting and parsing of monetize
and demonetize
with an optional options object. These values default to US centric currency values, but they can be overridden based on your locale.
monetize(1.23) // -> "$1.23"
monetize(1.23, { symbol: '€' }) // -> "€1.23"
The number of digits to be grouped into a single grouping.
monetize(1000.23) // -> "$1,000.23"
monetize(1000.23, { groups: 2 }) // -> "$10,00.23"
Separator between the number groupings.
monetize(1000.23) // -> "$1,000.23"
monetize(1000.23, { separator: "" }) // -> "$1000.23"
monetize(1000.23, { separator: " " }) // -> "$1 000.23"
monetize(1.23) // -> "$1.23"
monetize(1.23, { decimal: "," }) // -> "$1,23"
Number of decimal places to store as the fractional.
monetize(1.23) // -> "$1.23"
monetize(1.23, { precision: 0 }) // -> "$1"
monetize(1.23, { precision: 3 }) // -> "$1.230"
Allows you to customize the format pattern using !
as replacement for the currency symbol and #
as replacement for the currency amount.
monetize(1.23) // -> "$1.23"
monetize(1.23, { pattern: "# !" }) // -> "1.23 $"
monetize(1.23, { pattern: "<!#>" }) // -> "<$1.23>"
monetize(1.23, { pattern: "#" }) // -> "1.23"
Allows you to customize the negative format pattern using !
as replacement for the currency symbol and #
as replacement for the currency amount.
monetize(1.23) // -> "$1.23"
monetize(1.23, { negativePattern: "!-#" }) // -> "$-1.23"
monetize(1.23, { negativePattern: "-# !" }) // -> "-1.23 $"
monetize(1.23, { negativePattern: "(!#)" }) // -> "($1.23)"
monetize(1.23, { negativePattern: "-#" }) // -> "-1.23"