fixed-number
Format numbers, percentages and currencies with a fixed number of digits. Succinct wrapper of Intl.NumberFormat
defaulting to en-US
locale. In terms of functionality it sits between Number.prototype.toFixed
and the option-rich (and consequently verbose) Intl.NumberFormat
.
Example
const decimal = 1 3const percent = 2 1 'percent'const usd = 2 2 'usd'const eur = 3 0 'eur'const custom = 1 1 '+' console // '123,456.789'console // '-.---'console // '20.0%'console // '$19.99'console // '€020'console // '2.0+'
API
format = require('fixed-number')(..)
Arguments:
minimumIntegerDigits
(number, required)exactFractionDigits
(number, required)style
(string):'decimal'
(default),'percent'
, a ISO 4217 currency code, or a custom suffix.locale
(string): defaults to'en-US'
(see Node.js docs for requirements)
Returns a function format
that when called with a number, returns a formatted string.
Install
With npm do:
npm install fixed-number
License
MIT © 2017-present Vincent Weevers