format-currency
format-currency
is a JavaScript component to format numbers and strings to currency strings. Used in Exodus Ethereum Wallet.
Install
npm install --save format-currency
Notes
- Must have a JavaScript environment with
Object.assign
andIntl.NumberFormat
. In Node.js, this is at least v4. You can install in older environments, you'll just need to polyfill.
Usage
formatCurrency
Signature: formatCurrency(value, [options])
Parameters:
value
: Value to convert. Will pass through parse-num first. Will coerce anything to a number.options
: optionalobject
parameter to specify options. AppendingDigits
is not necessary. You can also shortenmaximum
tomax
andminimum
tomin
. Adds one more optionnanZero
, which when the number isNaN
, if it should be coerced to0
- defaults totrue
i.e.NaN => '0'
. Also availablecode
,symbol
,format
.
Returns:
A string
currency representation of the number.
Example:
const formatCurrency = // default, no currency symbol or codeconsole // => 10,000,000.15 // include the currency code 'USD'let opts = format: '%v %c' code: 'USD' console // => 10,000,000.15 USD // now include the currency symbollet opts = format: '%s%v %c' code: 'USD' symbol: '$' console // => $10,000,000.15 USD // use to reformat currency stringslet opts = format: '%s%v' symbol: '$' console // => $10,000,000.15 // change localelet opts = format: '%s%v' symbol: '$' locale: 'de-DE' // in Node.js (English only, unless you compiled with other language support)console // => $10,000,000.15// in Electron (renderer) or browser (supports all locales)console // => $10.000.000,15 // crypto currency (Bitcoin, Ethereum)let opts = format: '%v %c' code: 'BTC' maxFraction: 8 console // => 1.12345678 (notice only 8)
Related
- format-num: Format numbers / number strings to nice strings with grouping and decimal separators.
- number-unit: Numbers with units. Easily convert numbers to from different units.
- parse-num: Parse anything into a number. A dependency of this library.
License
MIT