Home Manual Reference Source Repository

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

toFixed(value: Float, precision: Number): String

Implementation of toFixed() that treats floats more like decimals.

public

unformat(value: String | Array<String>, decimal: Number, fallback: Float): Float

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: "$ " });

Params:

NameTypeAttributeDescription
list Array<Number>

Array of numbers to format

opts Object
  • optional
  • default: {}

Object containing all the options of the method

Return:

Array<String>

Array of accouting-formatted number strings of same length

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

Params:

NameTypeAttributeDescription
amount Number

Amount to be formatted

opts Object
  • optional
  • default: {}

Object containing all the options of the method

Return:

String

Given number properly formatted as money

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

Params:

NameTypeAttributeDescription
number Number

Number to be formatted

opts Object
  • optional
  • default: {}

Object containing all the options of the method

Return:

String

Given number properly formatted

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:

NameTypeAttributeDescription
value Float

Float to be treated as a decimal number

precision Number
  • optional
  • default: settings.precision

Number of decimal digits to keep

Return:

String

Given number transformed into a string with the given precission

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 (NaNs become 0 or provided by fallback value).

Alias: parse(value, decimal, fallback)

Usage:

unformat('£ 12,345,678.90 GBP');
// => 12345678.9

Params:

NameTypeAttributeDescription
value String | Array<String>

String or array of strings containing the number/s to parse

decimal Number
  • optional
  • default: settings.decimal

Number of decimal digits of the resultant number

fallback Float
  • optional
  • default: settings.fallback

Value returned on unformat() failure

Return:

Float

Parsed number