### Interactive Column Formatting Demo
Source: https://github.com/openexchangerates/accounting.js/blob/master/index.html
An interactive example demonstrating real-time column formatting as user input changes. It updates multiple formatted outputs based on a list of values.
```javascript
var $columnValues = $('#demo-column').find('input'),
$columnOutputs = $('#demo-column').find('.output'),
$columnOutputs2 = $('#demo-column').find('.output2');
$columnValues.bind('keydown keyup keypress focus blur paste', function() {
var list = $.map($columnValues, function(each) {
return $(each).val();
});
var formatted = accounting.formatColumn(list, {
format: "%s %v"
});
var formatted2 = accounting.formatColumn(list, {
symbol: "GBP",
precision: 0,
format: {
pos: "%s %v",
neg: "%s (%v)",
zero: "%s --"
}
});
$.each($columnOutputs, function(i, each) {
$(each).text(formatted[i]);
});
$.each($columnOutputs2, function(i, each) {
$(each).text(formatted2[i]);
});
});
```
--------------------------------
### formatColumn Example
Source: https://github.com/openexchangerates/accounting.js/blob/master/index.html
Formats an array of numbers for column display, aligning them with padding. Requires CSS 'white-space: pre' on the container.
```javascript
accounting.formatColumn([123.5, 3456.49, 777888.99, 12345678, -5432], "$ ");
```
--------------------------------
### Example with Data Manipulation
Source: https://github.com/openexchangerates/accounting.js/blob/master/tests/qunit/test.html
This snippet demonstrates a conditional data manipulation and display within an HTML list item. It's part of the accounting.js test suite.
```html
<% if (data) { data += 12345; }; %>
<%= data %>
```
--------------------------------
### Basic Script Inclusion and Usage
Source: https://github.com/openexchangerates/accounting.js/blob/master/index.html
Shows how to include the accounting.js library in an HTML file and use its basic functionality.
```html
```
--------------------------------
### formatMoney with Negative Values
Source: https://github.com/openexchangerates/accounting.js/blob/master/index.html
Demonstrates how to format negative currency values with a specified symbol and precision.
```javascript
accounting.formatMoney(-500000, "£ ", 0);
```
--------------------------------
### Default formatMoney Usage
Source: https://github.com/openexchangerates/accounting.js/blob/master/index.html
Formats a number into a currency string using default settings. Assumes USD currency.
```javascript
accounting.formatMoney(12345678);
```
--------------------------------
### Run Jasmine Specs with Trivial Reporter
Source: https://github.com/openexchangerates/accounting.js/blob/master/tests/jasmine/runner.html
Adds a trivial reporter to the Jasmine environment and then executes all defined specs.
```javascript
jasmine.getEnv().addReporter(new jasmine.TrivialReporter());
jasmine.getEnv().execute();
```
--------------------------------
### Extend Number Settings with Underscore.js
Source: https://github.com/openexchangerates/accounting.js/blob/master/index.html
Extends default number settings using _.defaults from Underscore.js. This can also be achieved with jQuery.extend.
```javascript
accounting.settings.number = _.defaults({
precision: 2,
thousand: " "
}, accounting.settings.number);
```
--------------------------------
### Default Settings for Currency
Source: https://github.com/openexchangerates/accounting.js/blob/master/index.html
Defines the default currency symbol, format string, decimal separator, thousands separator, and precision.
```javascript
accounting.settings = {
currency: {
symbol : "$",
format: "%s%v",
decimal : ".",
thousand: ",",
precision : 2
},
number: {
precision : 0,
thousand: ",",
decimal : "."
}
}
```
--------------------------------
### formatMoney with Custom Format String
Source: https://github.com/openexchangerates/accounting.js/blob/master/index.html
Allows control over currency symbol position using a format string. '%v' represents the value and '%s' the symbol.
```javascript
accounting.formatMoney(5318008, { symbol: "GBP", format: "%v %s" });
```
--------------------------------
### accounting.formatMoney()
Source: https://github.com/openexchangerates/accounting.js/blob/master/index.html
Formats any number into a currency string with customizable symbol, precision, and separators.
```APIDOC
## accounting.formatMoney()
### Description
Formats any number into a currency string with customizable currency symbol, precision (decimal places), and thousand/decimal separators.
### Method
`accounting.formatMoney(number, symbol, precision, decimalSeparator, thousandSeparator)`
### Parameters
- **number** (number) - The number to format.
- **symbol** (string, optional) - The currency symbol. Defaults to '$'.
- **precision** (number, optional) - The number of decimal places. Defaults to 2.
- **decimalSeparator** (string, optional) - The character used for the decimal point. Defaults to '.'.
- **thousandSeparator** (string, optional) - The character used for the thousands separator. Defaults to ','.
Alternatively, `symbol`, `precision`, `decimalSeparator`, and `thousandSeparator` can be provided as an options object:
`accounting.formatMoney(number, { symbol: "€", precision: 2, decimalSeparator: ",", thousandSeparator: "." })`
### Request Example
```javascript
// Default usage:
accounting.formatMoney(12345678);
// European formatting:
accounting.formatMoney(4999.99, "€", 2, ".", ",");
// Negative values:
accounting.formatMoney(-500000, "£ ", 0);
// Custom format string:
accounting.formatMoney(5318008, { symbol: "GBP", format: "%v %s" });
```
### Response
#### Success Response
- Returns a string representing the formatted currency value.
```
--------------------------------
### Format Money - Standard Usage
Source: https://github.com/openexchangerates/accounting.js/blob/master/index.html
Formats a number into a currency string using standard parameters: number, symbol, precision, thousand separator, decimal separator, and format.
```javascript
accounting.formatMoney(12345678); // $12,345,678.00
```
```javascript
accounting.formatMoney(4999.99, "€", 2, ".", ","); // €4.999,99
```
```javascript
accounting.formatMoney(-500000, "£ ", 0); // £ -500,000
```
--------------------------------
### Format Money - Options Object
Source: https://github.com/openexchangerates/accounting.js/blob/master/index.html
Formats a number into a currency string using an options object that specifies symbol, precision, thousand separator, decimal separator, and format.
```javascript
accounting.formatMoney(5318008, {
symbol: "GBP",
precision: 0,
thousand: "·",
format: {
pos : "%s %v",
neg : "%s (%v)",
zero: "%s -- "
}
});
```
--------------------------------
### accounting.formatMoney()
Source: https://github.com/openexchangerates/accounting.js/blob/master/index.html
Formats a number into a currency string, allowing for custom symbols, precision, separators, and format strings.
```APIDOC
## accounting.formatMoney(number_, [symbol = "$"], [precision = 2], [thousand = ","], [decimal = ""], [format = "%s%v"])
## accounting.formatMoney(number_, [options])
### Description
Formats a given number into a currency string. It can accept a number and several optional parameters for customization, or a single options object that mirrors the `settings.currency` structure.
### Parameters
* **number_** (number | array) - The number or array of numbers to format.
* **symbol** (string, optional) - The currency symbol to prepend or include based on the format. Defaults to "$".
* **precision** (number, optional) - The number of decimal places. Defaults to 2.
* **thousand** (string, optional) - The thousands separator. Defaults to ",".
* **decimal** (string, optional) - The decimal separator. Defaults to ".".
* **format** (string | object, optional) - The format string or object defining the layout of the currency value. Defaults to "%s%v". Can be an object with `pos`, `neg`, and `zero` properties.
* **options** (object, optional) - An object containing any of the above parameters (`symbol`, `precision`, `thousand`, `decimal`, `format`).
### Usage Examples
Standard usage:
```javascript
accounting.formatMoney(12345678); // "$12,345,678.00"
accounting.formatMoney(4999.99, "€", 2, ".", ","); // "€4.999,99"
accounting.formatMoney(-500000, "£ ", 0); // "£ -500,000"
```
Usage with options object:
```javascript
accounting.formatMoney(5318008, {
symbol: "GBP",
precision: 0,
housand: "·",
format: {
pos : "%s %v",
neg : "%s (%v)",
zero: "%s -- "
}
}); // "GBP 5,318,008"
```
Formatting an array of values:
```javascript
accounting.formatMoney([123, 456, [78, 9]], "$", 0); // ["$123", "$456", ["$78", "$9"]]
```
```
--------------------------------
### accounting.settings
Source: https://github.com/openexchangerates/accounting.js/blob/master/index.html
The settings object controls default parameters for library methods. These can be modified externally to change the default behavior of formatting functions.
```APIDOC
## accounting.settings
### Description
The `settings` object allows customization of default parameters for all accounting.js methods. It includes configurations for currency symbols, formats, decimal and thousand separators, and number precision.
### Usage
Settings can be accessed and modified directly. For example, to change the default currency format:
```javascript
accounting.settings.currency.format = "%s %v";
```
The `format` property can also be an object to specify different formats for positive, negative, and zero values:
```javascript
accounting.settings.currency.format = {
pos : "%s %v", // for positive values, eg. "$ 1.00" (required)
neg : "%s (%v)", // for negative values, eg. "$ (1.00)" _[optional]_
zero: "%s -- " // for zero values, eg. "$ --" _[optional]_
};
```
Defaults can be extended using utility functions like `_.defaults` (from Underscore.js) or `$.extend` (from jQuery):
```javascript
accounting.settings.number = _.defaults({
precision: 2,
housand: " "
}, accounting.settings.number);
```
### Default Structure
```javascript
accounting.settings = {
currency: {
symbol : "$",
format: "%s%v",
decimal : ".",
housand: ",",
precision : 2
},
number: {
precision : 0,
housand: ",",
decimal : "."
}
}
```
```
--------------------------------
### Currency Format as Object
Source: https://github.com/openexchangerates/accounting.js/blob/master/index.html
Defines currency format using an object with positive, negative, and zero value properties.
```javascript
accounting.settings.currency.format = {
pos : "%s %v",
neg : "%s (%v)",
zero: "%s -- "
};
```
--------------------------------
### Format Money - Array Recursion
Source: https://github.com/openexchangerates/accounting.js/blob/master/index.html
Recursively formats an array of numbers into currency strings.
```javascript
accounting.formatMoney([123, 456, [78, 9]], "$", 0); // ["$123", "$456", ["$78", "$9"]]
```
--------------------------------
### accounting.formatColumn()
Source: https://github.com/openexchangerates/accounting.js/blob/master/index.html
Formats a list of numbers into columns, applying currency formatting and optionally adding padding for alignment.
```APIDOC
## accounting.formatColumn(list_, [symbol = "$"], [precision = 2], [thousand = ","], [decimal = ""], [format = "%s%v"])
## accounting.formatColumn(list, [options])
### Description
Formats an array (or multi-dimensional array) of numbers, typically for display in columns. It applies currency formatting recursively and can handle alignment through padding.
### Parameters
* **list_** (array) - The list or multi-dimensional array of numbers to format.
* **symbol** (string, optional) - The currency symbol. If a space is appended (e.g., "$ "), it can be used for padding.
* **precision** (number, optional) - The number of decimal places. Defaults to 2.
* **thousand** (string, optional) - The thousands separator. Defaults to ",".
* **decimal** (string, optional) - The decimal separator. Defaults to ".".
* **format** (string | object, optional) - The format string or object defining the layout of the currency value. Defaults to "%s%v".
* **options** (object, optional) - An object containing any of the above parameters (`symbol`, `precision`, `thousand`, `decimal`, `format`).
### Usage Examples
Standard usage with padding:
```javascript
var list = [123, 12345];
accounting.formatColumn(list, "$ ", 0); // ["$ 123", "$ 12,345"]
```
Formatting multi-dimensional arrays:
```javascript
var list = [[1, 100], [900, 9]];
accounting.formatColumn(list); // [["$ 1.00", "$100.00"], ["$900.00", "$ 9.00"]]
```
Usage with options object:
```javascript
var list = [1000, -2000];
accounting.formatColumn(list, {
symbol: "€",
precision: 0,
format: {
pos: "%v %s",
neg: "(%v) %s"
}
}); // ["1.000 €", "(2.000) €"]
```
```
--------------------------------
### Format Column - Standard Usage with Padding
Source: https://github.com/openexchangerates/accounting.js/blob/master/index.html
Formats a list of numbers into a column of currency strings, allowing for padding by including a space in the symbol.
```javascript
var list = [123, 12345];
accounting.formatColumn(list, "$ ", 0); // ["$ 123", "$ 12,345"]
```
--------------------------------
### Update Currency Format String
Source: https://github.com/openexchangerates/accounting.js/blob/master/index.html
Allows external modification of the default currency format string.
```javascript
accounting.settings.currency.format = "%s %v";
```
--------------------------------
### accounting.toFixed()
Source: https://github.com/openexchangerates/accounting.js/blob/master/index.html
Provides better rounding for floating point numbers compared to the native toFixed().
```APIDOC
## accounting.toFixed()
### Description
An implementation of `toFixed()` that treats floating-point numbers more like decimal values, fixing inconsistent precision rounding issues found in standard JavaScript `toFixed()`.
### Method
`accounting.toFixed(number, precision)`
### Parameters
- **number** (number) - The floating-point number to round.
- **precision** (number) - The number of decimal places to round to.
### Request Example
```javascript
accounting.toFixed(0.615, 2);
```
### Response
#### Success Response
- Returns a string representing the rounded number.
```
--------------------------------
### Format Number - Options Object
Source: https://github.com/openexchangerates/accounting.js/blob/master/index.html
Formats a number using an options object for precision and thousand separator.
```javascript
accounting.formatNumber(5318008, {
precision : 3,
thousand : " "
});
```
--------------------------------
### toFixed Precision Issue in JavaScript
Source: https://github.com/openexchangerates/accounting.js/blob/master/index.html
Demonstrates the inconsistent rounding behavior of JavaScript's native toFixed() method with certain floating-point numbers.
```javascript
(0.615).toFixed(2);
```
--------------------------------
### Format Number - Standard Usage
Source: https://github.com/openexchangerates/accounting.js/blob/master/index.html
Formats a number with specified precision, thousand separator, and decimal separator.
```javascript
accounting.formatNumber(9876543); // 9,876,543
```
```javascript
accounting.formatNumber(4999.99, 2, ".", ","); // 4.999,99
```
--------------------------------
### accounting.toFixed()
Source: https://github.com/openexchangerates/accounting.js/blob/master/index.html
Rounds a number to a specified number of decimal places and returns it as a string.
```APIDOC
## accounting.toFixed(number_, [precision = 0])
### Description
Rounds a number to a specified number of decimal places and returns the result as a string. This function provides a more predictable rounding behavior compared to the native JavaScript `Number.toFixed()` method, especially for values that might have floating-point inaccuracies.
### Parameters
* **number_** (number) - The number to round.
* **precision** (number, optional) - The number of decimal places to round to. Defaults to 0.
### Usage Examples
```javascript
accounting.toFixed(0.615, 2); // "0.62"
// Compare to native JavaScript:
(0.615).toFixed(2); // "0.61"
```
```
--------------------------------
### Custom formatMoney with European Formatting
Source: https://github.com/openexchangerates/accounting.js/blob/master/index.html
Formats a number with a custom currency symbol and specific decimal/thousand separators. Useful for European locales.
```javascript
accounting.formatMoney(4999.99, "€", 2, ".", ",");
```
--------------------------------
### accounting.formatColumn()
Source: https://github.com/openexchangerates/accounting.js/blob/master/index.html
Formats a list of numbers or money values for column-display, aligning symbols and decimals.
```APIDOC
## accounting.formatColumn()
### Description
Formats a list of numbers and money values with padding to align currency symbols and decimal places, suitable for column display. Requires the containing element to be styled with `white-space: pre`.
### Method
`accounting.formatColumn(numbers, symbol)`
### Parameters
- **numbers** (array) - An array of numbers or money strings to format.
- **symbol** (string, optional) - The currency symbol to use for formatting. Defaults to '$ '.
### Request Example
```javascript
accounting.formatColumn([123.5, 3456.49, 777888.99, 12345678, -5432], "$ ");
```
### Response
#### Success Response
- Returns an array of strings, each representing a formatted value aligned for column display.
```
--------------------------------
### accounting.toFixed for Better Rounding
Source: https://github.com/openexchangerates/accounting.js/blob/master/index.html
Provides a more accurate implementation of toFixed() that treats floating-point numbers more like decimal values, ensuring consistent rounding.
```javascript
accounting.toFixed(0.615, 2);
```
--------------------------------
### Format Number - Array Recursion
Source: https://github.com/openexchangerates/accounting.js/blob/master/index.html
Recursively formats an array of numbers.
```javascript
accounting.formatNumber([123456, [7890, 123]]); // ["123,456", ["7,890", "123"]]
```
--------------------------------
### unformat Usage
Source: https://github.com/openexchangerates/accounting.js/blob/master/index.html
Parses a formatted currency string and returns the unformatted numerical value. Also aliased as accounting.parse().
```javascript
accounting.unformat("£ 12,345,678.90 GBP");
```
--------------------------------
### Format Column - Multi-dimensional Array
Source: https://github.com/openexchangerates/accounting.js/blob/master/index.html
Applies formatting recursively to a multi-dimensional array of numbers for column display.
```javascript
var list = [[1, 100], [900, 9]];
accounting.formatColumn(list); // [[`$ 1.00`, `$100.00`], [`$900.00`, `$ 9.00`]]
```
--------------------------------
### To Fixed Precision
Source: https://github.com/openexchangerates/accounting.js/blob/master/index.html
Rounds a number to a specified number of decimal places and returns it as a string. Compares to JavaScript's native Number.toFixed().
```javascript
accounting.toFixed(0.615, 2); // "0.62"
```
```javascript
(0.615).toFixed(2); // "0.61"
```
--------------------------------
### Default formatNumber Usage
Source: https://github.com/openexchangerates/accounting.js/blob/master/index.html
Formats a number with default precision and thousand separators. Removes any existing currency formatting.
```javascript
accounting.formatNumber(5318008);
```
--------------------------------
### accounting.unformat()
Source: https://github.com/openexchangerates/accounting.js/blob/master/index.html
Removes currency symbols and formatting from a string, returning the numerical value.
```APIDOC
## accounting.unformat(string_, [decimal])
### Description
Converts a formatted string (e.g., currency string) back into a plain number. It can intelligently handle different decimal separators if specified.
### Parameters
* **string_** (string) - The string to unformat.
* **decimal** (string, optional) - The decimal separator used in the string. This is necessary if the decimal separator is not the default "." (e.g., if it's a comma ",").
### Usage Examples
Standard unformatting:
```javascript
accounting.unformat("GBP £ 12,345,678.90"); // 12345678.9
```
Unformatting with a non-standard decimal separator:
```javascript
accounting.unformat("€ 1.000.000,00", ","); // 1000000
```
```
--------------------------------
### Unformat String - Standard
Source: https://github.com/openexchangerates/accounting.js/blob/master/index.html
Converts a formatted currency or number string back into a number.
```javascript
accounting.unformat("GBP £ 12,345,678.90"); // 12345678.9
```
--------------------------------
### accounting.formatNumber()
Source: https://github.com/openexchangerates/accounting.js/blob/master/index.html
Formats a number with specified precision and separators, without adding a currency symbol.
```APIDOC
## accounting.formatNumber(number_, [precision = 0], [thousand = ","], [decimal = ""])
## accounting.formatNumber(number_, [object])
### Description
Formats a number using specified precision, thousands separator, and decimal separator. It does not include currency symbols or currency-specific formatting.
### Parameters
* **number_** (number | array) - The number or array of numbers to format.
* **precision** (number, optional) - The number of decimal places. Defaults to 0.
* **thousand** (string, optional) - The thousands separator. Defaults to ",".
* **decimal** (string, optional) - The decimal separator. Defaults to ".".
* **object** (object, optional) - An object containing `precision`, `thousand`, and `decimal` properties, matching `settings.number`.
### Usage Examples
Standard usage:
```javascript
accounting.formatNumber(9876543); // "9,876,543"
accounting.formatNumber(4999.99, 2, ".", ","); // "4.999,99"
```
Usage with options object:
```javascript
accounting.formatNumber(5318008, {
precision : 3,
housand: " "
}); // "5 318 008.000"
```
Formatting an array of values:
```javascript
accounting.formatNumber([123456, [7890, 123]]); // ["123,456", ["7,890", "123"]]
```
```
--------------------------------
### accounting.formatNumber()
Source: https://github.com/openexchangerates/accounting.js/blob/master/index.html
Formats a number with custom precision and localization, including separated thousands.
```APIDOC
## accounting.formatNumber()
### Description
Formats any number or array of numbers with separated thousands and custom precision. It first unformats the input using `accounting.unformat()`.
### Method
`accounting.formatNumber(number, precision, thousandSeparator)`
### Parameters
- **number** (number or array) - The number or array of numbers to format.
- **precision** (number, optional) - The number of decimal places. Defaults to 0.
- **thousandSeparator** (string, optional) - The character used for the thousands separator. Defaults to ','.
### Request Example
```javascript
accounting.formatNumber(5318008);
accounting.formatNumber(9876543.21, 3, " ");
```
### Response
#### Success Response
- Returns a string representing the formatted number.
```
--------------------------------
### Format Column of Numbers
Source: https://github.com/openexchangerates/accounting.js/blob/master/index.html
Formats an array of numbers into a column of strings using specified formatting options. Useful for displaying lists of monetary values.
```javascript
var numbers = [123.5, 3456.615, 777888.99, -5432, -1234567, 0];
// Basic formatting with currency symbol and space
var formatted = accounting.formatColumn(numbers, "$ ");
// Custom formatting with symbol, precision, and specific formats for positive, negative, and zero values
var different = accounting.formatColumn(numbers, {
symbol: "HK$",
precision: 0,
format: {
pos: "%s %v",
neg: "%s (%v)",
zero: "%s --"
}
});
// European style formatting with different thousand and decimal separators
var european = accounting.formatColumn(numbers, {
symbol: '€ ',
thousand: '.',
decimal: ','
});
// Formatting with symbol after the value
var symbolAfter = accounting.formatColumn(numbers, {
symbol: "GBP",
format: "%v %s"
});
// Example of appending to an HTML table
for ( var i = 0; i < numbers.length; i++ ) {
$('| '+numbers[i]+' | '+formatted[i]+' | '+different[i]+' | '+european[i]+' | '+symbolAfter[i]+' |
').appendTo('table#demo-table tbody');
}
```
--------------------------------
### formatNumber with Custom Precision and Separator
Source: https://github.com/openexchangerates/accounting.js/blob/master/index.html
Formats a number with a specified precision (decimal places) and a custom thousand separator.
```javascript
accounting.formatNumber(9876543.21, 3, " ");
```
--------------------------------
### accounting.unformat()
Source: https://github.com/openexchangerates/accounting.js/blob/master/index.html
Parses a value from any formatted number or currency string, removing all formatting.
```APIDOC
## accounting.unformat()
### Description
Parses a value from any formatted number or currency string, removing all currency formatting and returning a plain number. Aliased as `accounting.parse()`.
### Method
`accounting.unformat(value)`
### Parameters
- **value** (string) - The formatted number or currency string to parse.
### Request Example
```javascript
accounting.unformat("£ 12,345,678.90 GBP");
```
### Response
#### Success Response
- Returns a number representing the unformatted value.
```
--------------------------------
### Format Individual Money Value
Source: https://github.com/openexchangerates/accounting.js/blob/master/index.html
Formats a single number into a money string with specified symbol, precision, thousand separator, and decimal separator. This is useful for displaying a single monetary amount.
```javascript
var $demoValue = $('#demo-number-value'),
$demoSymbol = $('#demo-number-symbol'),
$demoResult = $('#demo-number-result');
$demoValue.add($demoSymbol).bind('keydown keyup keypress focus blur paste change', function() {
var symbol = $demoSymbol.find(':selected').val();
var result = accounting.formatMoney($demoValue.val(), symbol, 2, ($demoSymbol.find(':selected').data('locale') === 'european') ? "." : ",", ($demoSymbol.find(':selected').data('locale') === 'european') ? "," : ".");
$demoResult.text(result);
});
```
--------------------------------
### Unformat String - Custom Decimal
Source: https://github.com/openexchangerates/accounting.js/blob/master/index.html
Unformats a string using a custom decimal separator when it differs from the default.
```javascript
accounting.unformat("€ 1.000.000,00", ","); // 1000000
```
=== COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.