format-number
v3.0.0
Published
Formats numbers with separators every 3 digits and currency etc. Highly configurable.
Downloads
197,027
Readme
format-number
Highly configurable formatter that expects a valid number in 'computer' format and accepts the following as options for formatting
Options
negativeType
string: 'right','left','brackets','none'; default = 'left' (note only used for setting of default symbols)negativeLeftSymbol
string: default = '-' ifnegativeType
is 'left', '(' ifnegativeType
is 'brackets' and '' otherwisenegativeRightSymbol
string: default = '-' ifnegativeType
is 'right', ')' ifnegativeType
is 'brackets' and '' otherwisenegativeLeftOut
boolean: whether the left symbol should be outside, ie precede the prefix; default = truenegativeRightOut
boolean: whether the right symbol should be outside, ie follow the suffix; default = trueprefix
string: default = ''suffix
string: default = ''integerSeparator
string: to be used as the thousands separator; default = ','decimalsSeparator
string: to be used as the thousanths separator; default = ''decimal
string: char to be used as decimal point; default = '.'padLeft
number: leading 0s will be added to left of number to make integer part this length; default = -1 /no paddingpadRight
number: trailing 0s will be added; default = -1 /no paddinground
number: number of decimal places to round to (rounds to nearest integer, mid point rounds away from zero ie 3.55 ~ 3.6 to 1dp, -3.55 ~ -3.6 to 1dp; default = no roundingtruncate
number: number of decimal places to truncate (3.58 truncates to 3.5 for 1dp, 3 for 0dp); default = no truncating
Override Options
noUnits
boolean: if true will override and leave out prefix and suffix; default= falsenoSeparator
- boolean: if true will override both integer and decimals separator and leave them out
Usage
var format=require('format-number');
var formattedNumber = format({prefix: '£', suffix: '/item'})(68932, {noSeparator: true});
or
var format=require('format-number');
var myFormat = format({prefix: '£', suffix: '/item'});
var formattedNumber = myFormat(68932, {noSeparator: true});
will both set formattedNumber to '£68932/item'
The override options can be ommitted:
var format=require('format-number');
var formattedNumber = format({prefix: '£', suffix: '/item'})(68932);
returns '£68,932/item'