1
+ /// <reference path="accounting.d.ts"/>
2
+
3
+ // formatMoney
4
+
5
+ // Default usage:
6
+ accounting . formatMoney ( 12345678 ) ; // $12,345,678.00
7
+
8
+ // European formatting (custom symbol and separators), could also use options object as second param:
9
+ accounting . formatMoney ( 4999.99 , "€" , 2 , "." , "," ) ; // €4.999,99
10
+
11
+ // Negative values are formatted nicely, too:
12
+ accounting . formatMoney ( - 500000 , "£ " , 0 ) ; // £ -500,000
13
+
14
+ // Simple `format` string allows control of symbol position [%v = value, %s = symbol]:
15
+ accounting . formatMoney ( 5318008 , { symbol : "GBP" , format : "%v %s" } ) ; // 5,318,008.00 GBP
16
+
17
+ // Example usage with options object:
18
+ accounting . formatMoney ( 5318008 , {
19
+ symbol : "GBP" ,
20
+ precision : 0 ,
21
+ thousand : "·" ,
22
+ format : {
23
+ pos : "%s %v" ,
24
+ neg : "%s (%v)" ,
25
+ zero : "%s --"
26
+ }
27
+ } ) ;
28
+
29
+ // Will recursively format an array of values:
30
+ accounting . formatMoney ( [ 123 , 456 , [ 78 , 9 ] ] , "$" , 0 ) ; // ["$123", "$456", ["$78", "$9"]]
31
+
32
+
33
+
34
+ // formatColumn
35
+
36
+ // Format list of numbers for display:
37
+ accounting . formatColumn ( [ 123.5 , 3456.49 , 777888.99 , 12345678 , - 5432 ] , "$ " ) ;
38
+
39
+ // Example usage (NB. use a space after the symbol to add arbitrary padding to all values):
40
+ accounting . formatColumn ( [ 123 , 12345 ] , "$ " , 0 ) ; // ["$ 123", "$ 12,345"]
41
+
42
+ // List of numbers can be a multi-dimensional array (formatColumn is applied recursively):
43
+ accounting . formatColumn ( [ [ 1 , 100 ] , [ 900 , 9 ] ] ) ; // [["$ 1.00", "$100.00"], ["$900.00", "$ 9.00"]]
44
+
45
+
46
+
47
+ // formatNumber
48
+
49
+ // Example usage:
50
+ accounting . formatNumber ( 5318008 ) ; // 5,318,008
51
+ accounting . formatNumber ( 9876543.21 , 3 , " " ) ; // 9 876 543.210
52
+ accounting . formatNumber ( 4999.99 , 2 , "." , "," ) ; // 4.999,99
53
+
54
+ // Example usage with options object:
55
+ accounting . formatNumber ( 5318008 , {
56
+ precision : 3 ,
57
+ thousand : " "
58
+ } ) ;
59
+
60
+ // Will recursively format an array of values:
61
+ accounting . formatNumber ( [ 123456 , [ 7890 , 123 ] ] ) ; // ["123,456", ["7,890", "123"]]
62
+
63
+
64
+
65
+ // toFixed
66
+
67
+ ( 0.615 ) . toFixed ( 2 ) ; // "0.61"
68
+ accounting . toFixed ( 0.615 , 2 ) ; // "0.62"
69
+
70
+
71
+
72
+
73
+ // unformat
74
+
75
+ // Example usage:
76
+ accounting . unformat ( "£ 12,345,678.90 GBP" ) ; // 12345678.9
77
+ accounting . unformat ( "GBP £ 12,345,678.90" ) ; // 12345678.9
78
+
79
+ // If a non-standard decimal separator was used (eg. a comma) unformat() will need it in order to work out
80
+ // which part of the number is a decimal/float:
81
+ accounting . unformat ( "€ 1.000.000,00" , "," ) ; // 1000000
82
+
83
+ // Settings object that controls default parameters for library methods:
84
+ accounting . settings = {
85
+ currency : {
86
+ symbol : "$" , // default currency symbol is '$'
87
+ format : "%s%v" , // controls output: %s = symbol, %v = value/number (can be object: see below)
88
+ decimal : "." , // decimal point separator
89
+ thousand : "," , // thousands separator
90
+ precision : 2 // decimal places
91
+ } ,
92
+ number : {
93
+ precision : 0 , // default precision on numbers is 0
94
+ thousand : "," ,
95
+ decimal : "."
96
+ }
97
+ } ;
98
+
99
+ // These can be changed externally to edit the library's defaults:
100
+ accounting . settings . currency . format = "%s %v" ;
101
+
102
+ // Format can be an object, with `pos`, `neg` and `zero`:
103
+ accounting . settings . currency . format = {
104
+ pos : "%s %v" , // for positive values, eg. "$ 1.00" (required)
105
+ neg : "%s (%v)" , // for negative values, eg. "$ (1.00)" [optional]
106
+ zero : "%s -- " // for zero values, eg. "$ --" [optional]
107
+ } ;
0 commit comments