Home Manual Reference Source Repository

Function

Static Public Summary
public

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

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

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

public

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

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:

NameTypeAttributeDescription
format String
  • optional
  • default: "%s%v"

String with the format to apply, where %s is the currency symbol and %v is the value

Return:

Object

object represnting format (with pos, neg and zero attributes)

public checkPrecision(val: *, base: *): * source

Check and normalise the value of precision (must be positive integer).

Params:

NameTypeAttributeDescription
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:

NameTypeAttributeDescription
list Array<Number>

An array of numbers to format

opts Object
  • optional
  • default: {}

Object containing all the options of the method

symbol Object | String
  • optional
  • default: "$"

String with the currency symbol. For conveniency if can be an object containing all the options of the method

precision Integer
  • optional
  • default: 2

Number of decimal digits

thousand String
  • optional
  • default: ','

String with the thousands separator

decimal String
  • optional
  • default: "."

String with the decimal separator

format String
  • optional
  • default: "%s%v"

String with the format to apply, where %s is the currency symbol and %v is the value

Return:

Array<String>

array of accouting-formatted number strings of same length

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

Params:

NameTypeAttributeDescription
number Number

Number to be formatted

opts Object
  • optional
  • default: {}

Object containing all the options of the method

Return:

String

The 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: 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

Params:

NameTypeAttributeDescription
number Number

The number to be formatted

opts Object
  • optional
  • default: {}

Object containing all the options of the method

Return:

String

The given number properly formatted

public stripInsignificantZeros(str: *, decimal: *): * source

import stripInsignificantZeros from 'accounting-js/lib/internal/stripInsignificantZeros.js'

Params:

NameTypeAttributeDescription
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:

NameTypeAttributeDescription
value Float

The float to be treated as a decimal number

precision Number
  • optional
  • default: 2

The number of decimal digits to keep

Return:

String

The given number transformed into a string with the given precission

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 (NaNs become 0) but this may change in future.

 accounting.unformat("£ 12,345,678.90 GBP"); // 12345678.9

Params:

NameTypeAttributeDescription
value String | Array<String>

The string or array of strings containing the number/s to parse

decimal Number

Number of decimal digits of the resultant number

Return:

Float

The parsed number