Function
Static Public Summary | ||
public |
Format a list of numbers into an accounting column, padding with whitespace to line up currency symbols, thousand separators and decimals places. |
|
public |
formatMoney(amount: Number, opts: Object): String Format a number into currency. |
|
public |
formatNumber(number: Number, opts: Object): String Format a number, with comma-separated thousands and custom precision/decimal places. |
|
public |
Implementation of toFixed() that treats floats more like decimals. |
|
public |
Takes a string/array of strings, removes all formatting/cruft and returns the raw float value. |
Static Public
public formatColumn(list: Array<Number>, opts: Object): Array<String> source
import formatColumn from 'accounting-js/lib/formatColumn.js'
Format a list of numbers into an accounting column, padding with whitespace to line up currency symbols, thousand separators and decimals places.
Returns array of accouting-formatted number strings of same length.
NB: white-space:pre
CSS rule is required on the list container to prevent
browsers from collapsing the whitespace in the output strings.
Usage:
formatColumn([123.5, 3456.49, 777888.99, 12345678, -5432], { symbol: "$ " });
public formatMoney(amount: Number, opts: Object): String source
import formatMoney from 'accounting-js/lib/formatMoney.js'
Format a number into currency.
Usage:
// Default usage
formatMoney(12345678);
// => $12,345,678.00
// European formatting (custom symbol and separators)
formatMoney(4999.99, { symbol: "€", precision: 2, thousand: ".", decimal: "," });
// => €4.999,99
// Negative values can be formatted nicely
formatMoney(-500000, { symbol: "£ ", precision: 0 });
// => £ -500,000
// Simple `format` string allows control of symbol position (%v = value, %s = symbol)
formatMoney(5318008, { symbol: "GBP", format: "%v %s" });
// => 5,318,008.00 GBP
public formatNumber(number: Number, opts: Object): String source
import formatNumber from 'accounting-js/lib/formatNumber.js'
Format a number, with comma-separated thousands and custom precision/decimal places.
Alias: format(number, opts)
Usage:
// Default usage
formatNumber(5318008);
// => 5,318,008
// Custom format
formatNumber(9876543.21, { precision: 3, thousand: " " });
// => 9 876 543.210
public toFixed(value: Float, precision: Number): String source
import toFixed from 'accounting-js/lib/toFixed.js'
Implementation of toFixed() that treats floats more like decimals.
Fixes binary rounding issues (eg. (0.615).toFixed(2) === '0.61') that present problems for accounting- and finance-related software.
Usage:
// Native toFixed has rounding issues
(0.615).toFixed(2);
// => '0.61'
// With accounting-js
toFixed(0.615, 2);
// => '0.62'
Params:
Name | Type | Attribute | Description |
value | Float | Float to be treated as a decimal number |
|
precision | Number |
|
Number of decimal digits to keep |
public unformat(value: String | Array<String>, decimal: Number, fallback: Float): Float source
import unformat from 'accounting-js/lib/unformat.js'
Takes a string/array of strings, removes all formatting/cruft and returns the raw float value.
Decimal must be included in the regular expression to match floats (defaults to
settings.decimal
), so if the number uses a non-standard decimal
separator, provide it as the second argument.
Also matches bracketed negatives (eg. '$ (1.99)' => -1.99
).
Doesn't throw any errors (NaN
s become 0 or provided by fallback value).
Alias: parse(value, decimal, fallback)
Usage:
unformat('£ 12,345,678.90 GBP');
// => 12345678.9
Params:
Name | Type | Attribute | Description |
value | String | Array<String> | String or array of strings containing the number/s to parse |
|
decimal | Number |
|
Number of decimal digits of the resultant number |
fallback | Float |
|
Value returned on unformat() failure |
Return:
Float | Parsed number |