Function
Static Public Summary | ||
public |
checkCurrencyFormat(format: String): Object Parses a format string or object and returns format obj for use in rendering. |
|
public |
checkPrecision(val: *, base: *): * Check and normalise the value of precision (must be positive integer). |
|
public |
formatColumn(list: Array<Number>, opts: Object, symbol: Object | String, precision: Integer, thousand: String, decimal: String, format: String): Array<String> Format a list of numbers into an accounting column, padding with whitespace to line up currency symbols, thousand separators and decimals places. |
|
public |
formatMoney(number: 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 |
stripInsignificantZeros(str: *, decimal: *): * |
|
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 checkCurrencyFormat(format: String): Object source
import checkCurrencyFormat from 'accounting-js/lib/internal/checkCurrencyFormat.js'
Parses a format string or object and returns format obj for use in rendering.
format
is either a string with the default (positive) format, or object
containing pos
(required), neg
and zero
values.
Either string or format.pos must contain "%v" (value) to be valid.
Params:
Name | Type | Attribute | Description |
format | String |
|
String with the format to apply, where %s is the currency symbol and %v is the value |
public checkPrecision(val: *, base: *): * source
import checkPrecision from 'accounting-js/lib/internal/checkPrecision.js'
Check and normalise the value of precision (must be positive integer).
Params:
Name | Type | Attribute | Description |
val | * | ||
base | * |
Return:
* |
public formatColumn(list: Array<Number>, opts: Object, symbol: Object | String, precision: Integer, thousand: String, decimal: String, format: String): 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.
List should be an array of numbers.
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.
accounting.formatColumn([123.5, 3456.49, 777888.99, 12345678, -5432], { symbol: "$ " });
Params:
Name | Type | Attribute | Description |
list | Array<Number> | An array of numbers to format |
|
opts | Object |
|
Object containing all the options of the method |
symbol | Object | String |
|
String with the currency symbol. For conveniency if can be an object containing all the options of the method |
precision | Integer |
|
Number of decimal digits |
thousand | String |
|
String with the thousands separator |
decimal | String |
|
String with the decimal separator |
format | String |
|
String with the format to apply, where %s is the currency symbol and %v is the value |
public formatMoney(number: Number, opts: Object): String source
import formatMoney from 'accounting-js/lib/formatMoney.js'
Format a number into currency.
Usage: accounting.formatMoney(number, symbol, precision, thousandsSep, decimalSep, format) defaults: (0, '$', 2, ',', '.', '%s%v')
Localise by overriding the symbol, precision, thousand / decimal separators and format.
// Default usage
accounting.formatMoney(12345678); // $12,345,678.00
// European formatting (custom symbol and separators), can also use options object as second parameter
accounting.formatMoney(4999.99, { symbol: "€", precision: 2, thousand: ".", decimal: "," }); // €4.999,99
// Negative values can be formatted nicely:
accounting.formatMoney(-500000, { symbol: "£ ", precision: 0 }); // £ -500,000
// Simple `format` string allows control of symbol position (%v = value, %s = symbol):
accounting.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: accounting.format()
Localise by overriding the precision and thousand / decimal separators.
accounting.formatNumber(5318008); // 5,318,008
accounting.formatNumber(9876543.21, { precision: 3, thousand: " " }); // 9 876 543.210
public stripInsignificantZeros(str: *, decimal: *): * source
import stripInsignificantZeros from 'accounting-js/lib/internal/stripInsignificantZeros.js'
Params:
Name | Type | Attribute | Description |
str | * | ||
decimal | * |
Return:
* |
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.
(0.615).toFixed(2); // "0.61" (native toFixed has rounding issues)
accounting.toFixed(0.615, 2); // "0.62"
Params:
Name | Type | Attribute | Description |
value | Float | The float to be treated as a decimal number |
|
precision | Number |
|
The number of decimal digits to keep |
public unformat(value: String | Array<String>, decimal: Number): 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.
Alias: accounting.parse(string)
Decimal must be included in the regular expression to match floats (defaults to accounting.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) but this may change in future.
accounting.unformat("£ 12,345,678.90 GBP"); // 12345678.9
Return:
Float | The parsed number |