fixed-number
v2.0.0
Published
Format numbers, percentages and currencies with a fixed number of digits
Downloads
2
Maintainers
Readme
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 = require('fixed-number')(1, 3)
const percent = require('fixed-number')(2, 1, 'percent')
const usd = require('fixed-number')(2, 2, 'usd')
const eur = require('fixed-number')(3, 0, 'eur')
const custom = require('fixed-number')(1, 1, '+')
console.log(decimal(123456.78900001)) // '123,456.789'
console.log(decimal(NaN)) // '-.---'
console.log(percent(0.2)) // '20.0%'
console.log(usd(19.99)) // '$19.99'
console.log(eur(19.99)) // '€020'
console.log(custom(2)) // '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