From 29917fb2cc3086c8a9e7433f763c3db1c6e14228 Mon Sep 17 00:00:00 2001 From: Joss Crowcroft Date: Sun, 21 Aug 2011 12:24:22 +0800 Subject: [PATCH 01/58] demo page and styles --- index.html | 242 +++++++++++++++++++++++++++++++++++++++++++++++++++++ style.css | 187 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 429 insertions(+) create mode 100644 index.html create mode 100644 style.css diff --git a/index.html b/index.html new file mode 100644 index 0000000..61d887b --- /dev/null +++ b/index.html @@ -0,0 +1,242 @@ + + + + + accounting.js - money-formatting JavaScript library + + + + + +
+

accounting.js

+

accounting.js is a tiny JavaScript library for number money and currency formatting, with optional excel-style column rendering (to line up symbols and decimals). It's lightweight, fully localisable and has zero dependencies.

+

 

+ +
+ +
+

Library Methods

+

formatMoney() - format any number into currency

+

The most basic function of this library is money-formatting numbers, with currency symbol, precision (places), and thousand/decimal separators:

+
// Default usage and custom precision/symbol :
+accounting.formatMoney(12345678); // $12,345,678.00
+
+// European formatting:
+accounting.formatMoney(4999.99, "€", 2, ".", ",")); // €4.999,99
+
+// It works for negative values, too:
+accounting.formatMoney(-500000, "£ ", 0) ); // £ -500,000
+

formatColumn() - format a list of values for column-display

+

This table demonstrates how accounting.js can take a list of numbers and money-format them with padding to line up currency symbols and decimal places (NB: white-space:pre is needed for the browser to render the padded spaces):

+ + + + + + + + + + + +
Original Number:With accounting.js:Different settings:European format:Symbol after value:
+
// Format list of numbers for display:
+accounting.formatColumn([123.5, 3456.49, 777888.99, 12345678, -5432], "$ ");
+ +

formatNumber() - format a number with custom precision and localisation

+

The base function of the library, which takes any number or array of numbers, runs accounting.unformat() to remove any formatting, and returns the number(s) formatted with separated thousands and custom precision:

+
accounting.formatNumber(9876543); // 9,876,543
+ +

unformat() - get a value from any formatted number/currency string

+

Takes any number and removes all currency formatting:

+
var price = accounting.formatMoney(12345678.90, "GBP £ "); // GBP £ 12,345,678.90
+
+// Remove currency formatting and get value:
+var raw = accounting.unformat(price); // 12345678.9
+
+ +
+

Demo / Try it out

+ +

Money formatting:

+
+

Enter any number into the box and choose currency. Uses accounting.formatMoney():

+

+ + +

+

Result: $ 0.00

+
+ +

Column formatting:

+
+

Edit the values in the table to see how formatColumn() keeps them aligned:

+ + + + + + + + + + + + + + + +
$ 1,000,000.00
$ -5,000.00
$ 0.00
+
+
+ +
+

Instructions / How to use

+

1. Download the script and put it somewhere, then do this:

+
<script src="path/to/accounting.js"></script>
+
+<script type="text/javascript">
+	// You can do this now:
+	accounting.formatMoney(5318008);
+</script>
+

2. Check out the documentation for full method/parameter info if you get stuck.

+
+ +
+

Documentation

+

Haven't had a chance to write detailed documentation yet, check back in about 72 hours.

+
+ +
+

Roadmap

+

Next Version:

+ +
+ +
+

Feedback / Support

+

Please create issues on the accounting.js Github repository if you have feedback or need support, or comment on Joss's blog.

+
+ +
+

Download

+

Hold up a sec

+
+ +
+
+
+

 

+

Moar Info

+

accounting.js was made by Joss (that's me) in Hong Kong, originally for internal use in Demand Analytics' enterprise application. It seemed to be working pretty well, so I'm releasing it in the hope that other people and apps can make use of and contribute to it.

+

I'm on them intertweets (@josscrowcroft). Any questions? Just email me here or post on the accounting.js Github repository.

+
+ + + + + + + + + + \ No newline at end of file diff --git a/style.css b/style.css new file mode 100644 index 0000000..6298cf7 --- /dev/null +++ b/style.css @@ -0,0 +1,187 @@ +/* Borrowed from Markdown.css: http://kevinburke.bitbucket.org/markdowncss/markdown.css */ +body{ + margin: 0 auto; + font-family: Georgia, Palatino, serif; + color: #444444; + line-height: 1; + max-width: 960px; + padding: 30px; +} +h1, h2, h3, h4 { + color: #111111; + font-weight: 400; +} +h1, h2, h3, h4, h5, p, ul, ol { + margin-bottom: 24px; + padding: 0; +} +h1 { + font-size: 48px; +} +h2 { + font-size: 36px; + margin: 24px 0 6px; +} +h3 { + font-size: 24px; +} +h4 { + font-size: 21px; +} +h5 { + font-size: 18px; +} +a { + color: #0099ff; + margin: 0; + padding: 0; + vertical-align: baseline; +} +li { + line-height: 24px; +} +li ul, li ul { + margin-left: 24px; +} +p, ul, ol { + font-size: 16px; + line-height: 24px; + max-width: 540px; +} +pre { + padding: 0px 24px; + max-width: 800px; + white-space: pre-wrap; +} +code { + font-family: Consolas, Monaco, Andale Mono, monospace; + line-height: 1.5; + font-size: 13px; +} +aside { + display: block; + float: right; + width: 390px; +} +blockquote { + margin: 1em 2em; + max-width: 476px; +} +blockquote p { + color: #666; + max-width: 460px; +} +hr { + margin: 0; + padding:0; + border:0; + outline:0; + height:2px; + background: #bbb; +} + + +/* Some overrides: */ +/* Thx to http://twitter.github.com/bootstrap/ */ + +h1 { font-size:40px; } +h2 { font-size:30px; } +h3 { font-size:22px; } +h4 { font-size:18px; padding-top:10px;} +h5 { font-size:1em; margin-bottom:1em; } +p, ul, ol { max-width: 580px; } +ol, ul { padding-left:24px; font-size:0.85em } +ol#list { font-size:1em } +a:visited { color:#5bf; } +a:hover, a:focus { outline:none; color:#e25525 } + +section { + padding:0 0 10px; +} +table{width:100%;margin-bottom:18px;padding:0;text-align:left;border-collapse:separate;font-size:13px;}table th,table td{padding:10px 10px 9px;line-height:13.5px;vertical-align:middle;border-bottom:1px solid #ddd;} +table th{padding-top:9px;font-weight:bold;border-bottom-width:2px;} + +td { + white-space:pre; + font-family:monospace; +} + +pre { + background-color: whiteSmoke; + display: block; + padding: 17px; + margin: 0 0 22px; + line-height: 18px; + border: 1px solid rgba(0, 0, 0, 0.15); + -webkit-border-radius: 3px; + -moz-border-radius: 3px; + border-radius: 3px; + white-space: pre-wrap; + overflow:hidden; +} +code, pre { + padding: 0 3px 2px; + font-family: Consolas, Monaco, "Andale Mono", "Courier New", monospace; + font-size: 13px; + -webkit-border-radius: 3px; + -moz-border-radius: 3px; + border-radius: 3px; +} + +pre.prettyprint { + background: #fefbf3; + padding: 9px; + border: 1px solid rgba(0,0,0,.2); + -webkit-box-shadow: 0 1px 2px rgba(0,0,0,.1); + -moz-box-shadow: 0 1px 2px rgba(0,0,0,.1); + box-shadow: 0 1px 2px rgba(0,0,0,.1); +} + +.pln{color:#222} +@media screen{ + .str{color:#080} + .kwd{color:#008} + .com{color:#800} + .typ{color:#606} + .lit{color:#066} + .pun, + .opn, + .clo{color:#660} + .tag{color:#008} + .atn{color:#606} + .atv{color:#080} + .dec, + .var{color:#606} + .fun{color:red} +} +@media print,projection{ + .str{color:#060} + .kwd{color:#006;font-weight:bold} + .com{color:#600;font-style:italic} + .typ{color:#404;font-weight:bold} + .lit{color:#044} + .pun, + .opn, + .clo{color:#440} + .tag{color:#006;font-weight:bold} + .atn{color:#404} + .atv{color:#060} +} + + +div.well { + background: whiteSmoke; + margin: 20px 0; + padding: 5px 12px; + min-height: 20px; + border: 1px solid #DDD; + -webkit-border-radius: 4px; + -moz-border-radius: 4px; + border-radius: 4px; + -webkit-box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.05); + -moz-box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.05); + box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.05); +} +.well p { + max-width:100%; +} \ No newline at end of file From 78acf6548726233577b8f0ab80fc62b58e4167da Mon Sep 17 00:00:00 2001 From: Joss Crowcroft Date: Sun, 21 Aug 2011 12:25:19 +0800 Subject: [PATCH 02/58] demo page and styles --- readme.md | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 readme.md diff --git a/readme.md b/readme.md new file mode 100644 index 0000000..ad4735c --- /dev/null +++ b/readme.md @@ -0,0 +1,3 @@ +*accounting.js* is a tiny JavaScript library for number money and currency formatting, with optional excel-style column rendering (to line up symbols and decimals). It's lightweight, fully localisable and has zero dependencies. + +More info soon. \ No newline at end of file From c206a399011da35f176424fc7a25b4a21fd22a78 Mon Sep 17 00:00:00 2001 From: Joss Crowcroft Date: Sun, 21 Aug 2011 12:39:56 +0800 Subject: [PATCH 03/58] demo page and styles --- index.html | 26 ++++++++++++-------------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/index.html b/index.html index 61d887b..e3d40d2 100644 --- a/index.html +++ b/index.html @@ -1,10 +1,12 @@ - + - + accounting.js - money-formatting JavaScript library - - + + @@ -140,7 +142,7 @@

Feedback / Support

Download

-

Hold up a sec

+

accounting.js - Latest version from Github

@@ -151,20 +153,16 @@

Moar Info

accounting.js was made by Joss (that's me) in Hong Kong, originally for internal use in Demand Analytics' enterprise application. It seemed to be working pretty well, so I'm releasing it in the hope that other people and apps can make use of and contribute to it.

I'm on them intertweets (@josscrowcroft). Any questions? Just email me here or post on the accounting.js Github repository.

- - - - - - + + + + + \ No newline at end of file From 6e84b31fc144467af2e312e5628219d799dbbff6 Mon Sep 17 00:00:00 2001 From: Joss Crowcroft Date: Sun, 21 Aug 2011 12:41:31 +0800 Subject: [PATCH 04/58] demo page and styles --- index.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.html b/index.html index e3d40d2..2cd8379 100644 --- a/index.html +++ b/index.html @@ -151,7 +151,7 @@

Download

 

Moar Info

accounting.js was made by Joss (that's me) in Hong Kong, originally for internal use in Demand Analytics' enterprise application. It seemed to be working pretty well, so I'm releasing it in the hope that other people and apps can make use of and contribute to it.

-

I'm on them intertweets (@josscrowcroft). Any questions? Just email me here or post on the accounting.js Github repository.

+

You can follow me on Twitter at @josscrowcroft. Any questions? Just email me here or post on the accounting.js Github repository.

From aded75013350f1c028e674f8bd0d9ce17a446843 Mon Sep 17 00:00:00 2001 From: Joss Crowcroft Date: Sun, 21 Aug 2011 12:48:09 +0800 Subject: [PATCH 05/58] demo page and styles --- index.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.html b/index.html index 2cd8379..74e73aa 100644 --- a/index.html +++ b/index.html @@ -142,7 +142,7 @@

Feedback / Support

Download

-

accounting.js - Latest version from Github

+

accounting.js - Latest version from Github

From 425285c4b61197ff45d09f3d5dd9a68151723427 Mon Sep 17 00:00:00 2001 From: Joss Crowcroft Date: Sun, 21 Aug 2011 12:48:29 +0800 Subject: [PATCH 06/58] demo page and styles --- index.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.html b/index.html index 74e73aa..d0a5b6b 100644 --- a/index.html +++ b/index.html @@ -12,7 +12,7 @@

accounting.js

-

accounting.js is a tiny JavaScript library for number money and currency formatting, with optional excel-style column rendering (to line up symbols and decimals). It's lightweight, fully localisable and has zero dependencies.

+

accounting.js is a tiny JavaScript library for number, money and currency formatting, with optional excel-style column rendering (to line up symbols and decimals). It's lightweight, fully localisable and has zero dependencies.

 

  • methods From cb543c045cd4f0a3d85d259d9b127d87d75b5f3b Mon Sep 17 00:00:00 2001 From: Joss Crowcroft Date: Sun, 21 Aug 2011 12:49:24 +0800 Subject: [PATCH 07/58] demo page and styles --- index.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.html b/index.html index d0a5b6b..d880b21 100644 --- a/index.html +++ b/index.html @@ -154,7 +154,7 @@

    Moar Info

    You can follow me on Twitter at @josscrowcroft. Any questions? Just email me here or post on the accounting.js Github repository.

- + From 937fc3a45b9ff720f20b43cb74041a797f835552 Mon Sep 17 00:00:00 2001 From: Joss Crowcroft Date: Sun, 21 Aug 2011 14:09:49 +0800 Subject: [PATCH 08/58] demo page and styles --- index.html | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/index.html b/index.html index d880b21..3a29936 100644 --- a/index.html +++ b/index.html @@ -5,7 +5,14 @@ accounting.js - money-formatting JavaScript library @@ -61,10 +68,7 @@

formatNumber() - format a number with custom precision and

unformat() - get a value from any formatted number/currency string

Takes any number and removes all currency formatting:

-
var price = accounting.formatMoney(12345678.90, "GBP £ "); // GBP £ 12,345,678.90
-
-// Remove currency formatting and get value:
-var raw = accounting.unformat(price); // 12345678.9
+
accounting.unformat("GBP £ 12,345,678.90"); // 12345678.9
@@ -131,6 +135,7 @@

Next Version:

  • Add several other general and excel-style money formatting methods
  • Add padding parameter to override amount of space between currency symbol and value
  • Add parameter to control digit grouping (allowing 4-digit columns, eg. "CNY 123,4567.89")
  • +
  • Make formatNumber() by default use the same precision as the provided number
  • Implement feedback
  • @@ -150,8 +155,9 @@

    Download


     

    Moar Info

    -

    accounting.js was made by Joss (that's me) in Hong Kong, originally for internal use in Demand Analytics' enterprise application. It seemed to be working pretty well, so I'm releasing it in the hope that other people and apps can make use of and contribute to it.

    -

    You can follow me on Twitter at @josscrowcroft. Any questions? Just email me here or post on the accounting.js Github repository.

    +

    accounting.js was made by Joss (that's me) in Hong Kong, originally for internal use in the Demand Analytics enterprise application. It seemed to be working pretty well, so I'm releasing it in the hope that other people and apps can make use of and contribute to it.

    +

    You can follow me on Twitter at @josscrowcroft, I mostly post about JS, HTML5, startups, Hong Kong and things I've built.

    +

    Any questions? Just email me here or post on the accounting.js Github repository.

    From 72449c70ff786e6393f4ae5a99c709e9ac3532b3 Mon Sep 17 00:00:00 2001 From: Joss Crowcroft Date: Sun, 21 Aug 2011 17:29:27 +0800 Subject: [PATCH 09/58] demo page and styles --- index.html | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/index.html b/index.html index 3a29936..5c9fa0a 100644 --- a/index.html +++ b/index.html @@ -147,7 +147,8 @@

    Feedback / Support

    Download

    -

    accounting.js - Latest version from Github

    +

    accounting.js - Latest version from Github (5.2kb)

    +

    accounting.min.js - Latest version from Github (1.4kb, minified)

    From 9c8904a5dad6b670cd5f1b6ea34df8e0ee7be158 Mon Sep 17 00:00:00 2001 From: Joss Crowcroft Date: Sun, 21 Aug 2011 17:30:29 +0800 Subject: [PATCH 10/58] demo page and styles --- index.html | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/index.html b/index.html index 5c9fa0a..f4f7601 100644 --- a/index.html +++ b/index.html @@ -147,8 +147,10 @@

    Feedback / Support

    Download

    -

    accounting.js - Latest version from Github (5.2kb)

    -

    accounting.min.js - Latest version from Github (1.4kb, minified)

    +
    From 91bb7977a6f44aed7b25f3679d111340d71dafe1 Mon Sep 17 00:00:00 2001 From: Joss Crowcroft Date: Sun, 21 Aug 2011 22:57:08 +0800 Subject: [PATCH 11/58] demo page and styles --- index.html | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/index.html b/index.html index f4f7601..2f12d6d 100644 --- a/index.html +++ b/index.html @@ -120,12 +120,12 @@

    Instructions / How to use

    // You can do this now: accounting.formatMoney(5318008); </script> -

    2. Check out the documentation for full method/parameter info if you get stuck.

    +

    2. Check out the documentation and source-code for full method/parameter info if you get stuck.

    Documentation

    -

    Haven't had a chance to write detailed documentation yet, check back in about 72 hours.

    +

    Check back in 72 hours for docs ... haven't had time to write them yet.

    @@ -138,6 +138,10 @@

    Next Version:

  • Make formatNumber() by default use the same precision as the provided number
  • Implement feedback
  • +

    Later:

    +
      +
    • Extend the formatColumn() method to be able to format an entire table (or multi-dimensional array) of numbers, for easier templating.
    • +
    @@ -158,7 +162,7 @@

    Download


     

    Moar Info

    -

    accounting.js was made by Joss (that's me) in Hong Kong, originally for internal use in the Demand Analytics enterprise application. It seemed to be working pretty well, so I'm releasing it in the hope that other people and apps can make use of and contribute to it.

    +

    accounting.js was made by Joss (that's me) in Hong Kong, for internal use in our enterprise analytics application at Demand Analytics. It seemed to be working pretty well, so I'm releasing it in the hope that other people and apps can make use of and contribute to it.

    You can follow me on Twitter at @josscrowcroft, I mostly post about JS, HTML5, startups, Hong Kong and things I've built.

    Any questions? Just email me here or post on the accounting.js Github repository.

    From f9ac715347b549faea4f834c9f02774c0c43159c Mon Sep 17 00:00:00 2001 From: Joss Crowcroft Date: Sun, 21 Aug 2011 22:58:06 +0800 Subject: [PATCH 12/58] demo page and styles --- index.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.html b/index.html index 2f12d6d..2a1b9fa 100644 --- a/index.html +++ b/index.html @@ -2,7 +2,7 @@ - accounting.js - money-formatting JavaScript library + accounting.js - format money / currency in JavaScript - - + + @@ -266,12 +315,11 @@

    Moar Info

    european = accounting.formatColumn(numbers, { symbol: '€ ', thousand:'.', - decimal:',', - precision:2, + decimal:',' }), symbolAfter = accounting.formatColumn(numbers, { - symbol : " GBP", - symbolAfter : true + symbol : "GBP", + format : "%v %s" }); // Concat some nasty demo HTML: @@ -304,7 +352,9 @@

    Moar Info

    $columnValues.bind('keydown keyup keypress focus blur paste', function() { var list = $.map( $columnValues, function(each) { return $(each).val(); } ), - formatted = accounting.formatColumn(list, "$ "); + formatted = accounting.formatColumn(list, { + format:"%s %v" + }); $.each($columnOutputs, function(i, each) { $(each).text(formatted[i]); From b7df17fe1249c76d6358001d1e20cf879a635eb7 Mon Sep 17 00:00:00 2001 From: Joss Crowcroft Date: Tue, 23 Aug 2011 20:39:01 +0800 Subject: [PATCH 19/58] updated homepage demos and docs --- index.html | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/index.html b/index.html index ba7f475..76f4565 100644 --- a/index.html +++ b/index.html @@ -44,7 +44,7 @@

    formatMoney() - format any number into currency

    accounting.formatMoney(4999.99, "€", ".", ","); // €4.999,99 // Negative values are formatted nicely, too: -accounting.formatMoney(-500000, "£ ", 0) ); // £ -500,000 +accounting.formatMoney(-500000, "£ ", 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 @@ -264,6 +264,7 @@

    Next Version:

  • Add padding parameter to override amount of space between currency symbol and value.
  • Add choice of rounding method (floor, ceil or round).
  • Make formatNumber() by default use the same precision as the provided number.
  • +
  • Create test suite
  • Implement feedback.
  • Later:

    From 1da79864055f08eb4055a2418588a0c3aa62b179 Mon Sep 17 00:00:00 2001 From: Joss Crowcroft Date: Tue, 23 Aug 2011 23:14:05 +0800 Subject: [PATCH 20/58] fixed bug in library homepage --- index.html | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/index.html b/index.html index 76f4565..fb2c7af 100644 --- a/index.html +++ b/index.html @@ -40,8 +40,8 @@

    formatMoney() - format any number into currency

    // Default usage:
     accounting.formatMoney(12345678); // $12,345,678.00
     
    -// European formatting (custom symbol and separators), optionally using second-param options object:
    -accounting.formatMoney(4999.99, "€", ".", ","); // €4.999,99
    +// European formatting (custom symbol and separators), could also use options object as second param:
    +accounting.formatMoney(4999.99, "€", 2, ".", ","); // €4.999,99
     
     // Negative values are formatted nicely, too:
     accounting.formatMoney(-500000, "£ ", 0); // £ -500,000
    
    From 1b695c818601492fa0f73da6d959b94d56db34a9 Mon Sep 17 00:00:00 2001
    From: Joss Crowcroft 
    Date: Tue, 23 Aug 2011 23:17:32 +0800
    Subject: [PATCH 21/58] moar library homepage
    
    ---
     index.html | 1 +
     1 file changed, 1 insertion(+)
    
    diff --git a/index.html b/index.html
    index fb2c7af..0eac31d 100644
    --- a/index.html
    +++ b/index.html
    @@ -4,6 +4,7 @@
     	
     	accounting.js - format money / currency in JavaScript
     	
    +	
     	
    +	
     	
     	
     
    @@ -315,10 +332,18 @@ 

    Moar Info

    // demo thangs: jQuery(document).ready(function($) { - var numbers = [123.5, 3456.49, 777888.99, 12345678, -5432]; + var numbers = [123.5, 3456.615, 777888.99, -5432, -1234567, 0]; // Use accounting.js to format the list of numbers several ways: var formatted = accounting.formatColumn(numbers, "$ "), - different = accounting.formatColumn(numbers, "HK$ ", 0), + different = accounting.formatColumn(numbers, { + symbol:"HK$", + precision:0, + format: { + pos : "%s %v", + neg : "%s (%v)", + zero : "%s --" + } + }), european = accounting.formatColumn(numbers, { symbol: '€ ', thousand:'.', @@ -355,17 +380,30 @@

    Moar Info

    // Try it yourself clicky column/list demo: var $columnValues = $('#demo-column').find('input'), - $columnOutputs = $('#demo-column').find('.output'); + $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(); } ), formatted = accounting.formatColumn(list, { - format:"%s %v" + format : "%s %v" + }), + 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]); + }); }); }); From 52c53dfc9958678076e5de7f8ececf83f116ea12 Mon Sep 17 00:00:00 2001 From: Joss Crowcroft Date: Wed, 5 Oct 2011 15:37:23 +0800 Subject: [PATCH 26/58] remove everything real quick --- index.html | 432 ----------------------------------------------------- readme.md | 3 - style.css | 186 ----------------------- 3 files changed, 621 deletions(-) delete mode 100644 index.html delete mode 100644 readme.md delete mode 100644 style.css diff --git a/index.html b/index.html deleted file mode 100644 index 50556c0..0000000 --- a/index.html +++ /dev/null @@ -1,432 +0,0 @@ - - - - - accounting.js - format money / currency in JavaScript - - - - - - -
    -

    accounting.js

    -

    accounting.js is a tiny JavaScript library for number, money and currency formatting, with optional excel-style column rendering (to line up symbols and decimals). It's lightweight, fully localisable and has zero dependencies.

    -

     

    - -
    - -
    -

    Library Methods

    -

    formatMoney() - format any number into currency

    -

    The most basic function of this library is money-formatting numbers, with currency symbol, precision (places), and thousand/decimal separators:

    -
    // Default usage:
    -accounting.formatMoney(12345678); // $12,345,678.00
    -
    -// European formatting (custom symbol and separators), could also use options object as second param:
    -accounting.formatMoney(4999.99, "€", 2, ".", ","); // €4.999,99
    -
    -// Negative values are formatted nicely, too:
    -accounting.formatMoney(-500000, "£ ", 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
    -

    formatColumn() - format a list of values for column-display

    -

    This table demonstrates how accounting.js can take a list of numbers and money-format them with padding to line up currency symbols and decimal places (NB: white-space:pre is needed for the browser to render the padded spaces):

    - - - - - - - - - - - -
    Original Number:With accounting.js:Different settings:European format:Symbol after value:
    -
    // Format list of numbers for display:
    -accounting.formatColumn([123.5, 3456.49, 777888.99, 12345678, -5432], "$ ");
    - -

    formatNumber() - format a number with custom precision and localisation

    -

    The base function of the library, which takes any number or array of numbers, runs accounting.unformat() to remove any formatting, and returns the number(s) formatted with separated thousands and custom precision:

    -
    accounting.formatNumber(5318008); // 5,318,008
    -accounting.formatNumber(9876543.21, 3, " "); // 9 876 543.210
    - -

    toFixed() - better rounding for floating point numbers

    -

    Implementation of toFixed() that treats floats more like decimal values than binary, fixing inconsistent precision rounding in JavaScript (where some .05 values round up, while others round down):

    -
    (0.615).toFixed(2); // "0.61"
    -accounting.toFixed(0.615, 2); // "0.62"
    - -

    unformat() - get a value from any formatted number/currency string

    -

    Takes any number and removes all currency formatting:

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

    Demo / Try it out

    - -

    Money formatting:

    -
    -

    Enter any number into the box and choose currency. Uses accounting.formatMoney():

    -

    - - -

    -

    Result: $ 0.00

    -
    - -

    Column formatting:

    -
    -

    Edit the values in the table to see how formatColumn() keeps them aligned:

    - - - - - - - - - - - - - - - - - - -
    $ 1,000,000.00GBP 1,000,000
    $ -5,000.00GBP (5,000)
    $ 0.00GBP --
    -
    -
    - - -
    -

    Instructions / How to use

    -

    1. Download the script and put it somewhere, then do this:

    -
    <script src="path/to/accounting.js"></script>
    -
    -<script type="text/javascript">
    -	// You can do this now:
    -	accounting.formatMoney(5318008);
    -</script>
    -

    2. Check out the documentation and source-code for full method/parameter info if you get stuck.

    -
    - - -
    -

    Documentation

    -

    Information on the parameters of each method. See library methods above for more examples. Optional parameters are in [italics], with the default value indicated.

    - - -

    accounting.settings

    -
    // Settings object that controls default parameters for library methods:
    -accounting.settings = {
    -	currency: {
    -		symbol : "$",   // default currency symbol is '$'
    -		format: "%s%v", // controls output: %s = symbol, %v = value/number (can be object: see below)
    -		decimal : ".",  // decimal point separator
    -		thousand: ",",  // thousands separator
    -		precision : 2   // decimal places
    -	},
    -	number: {
    -		precision : 0,  // default precision on numbers is 0
    -		thousand: ",",
    -		decimal : "."
    -	}
    -}
    -
    -// These can be changed externally to edit the library's defaults:
    -accounting.settings.currency.format = "%s %v";
    -
    -// Format can be an object, with `pos`, `neg` and `zero`:
    -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]
    -};
    -
    -// Example using underscore.js - extend default settings (also works with $.extend in jQuery):
    -accounting.settings.number = _.defaults({
    -	precision: 2,
    -	thousand: " "
    -}, accounting.settings.number);
    - - -

    accounting.formatMoney()

    -
    // Standard usage and parameters (returns string):
    -accounting.formatMoney(number,[symbol = "$"],[precision = 2],[thousand = ","],[decimal = "."],[format = "%s%v"])
    -
    -// Second parameter can be an object:
    -accounting.formatMoney(number, [options])
    -
    -// Available fields in options object, matching `settings.currency`:
    -var options = {
    -	symbol : "$",
    -	decimal : ".",
    -	thousand: ",",
    -	precision : 2,
    -	format: "%s%v"
    -};
    -
    -// Example usage:
    -accounting.formatMoney(12345678); // $12,345,678.00
    -accounting.formatMoney(4999.99, "€", 2, ".", ","); // €4.999,99
    -accounting.formatMoney(-500000, "£ ", 0); // £ -500,000
    -
    -// Example usage with options object:
    -accounting.formatMoney(5318008, {
    -	symbol: "GBP",
    -	precision: 0,
    -	thousand: "·",
    -	format: {
    -		pos : "%s %v",
    -		neg : "%s (%v)",
    -		zero: "%s  --"
    -	}
    -});
    -
    -// Will recursively format an array of values:
    -accounting.formatMoney([123, 456, [78, 9]], "$", 0); // ["$123", "$456", ["$78", "$9"]]
    - - - -

    accounting.formatColumn()

    -
    // Standard usage and parameters (returns array):
    -accounting.formatColumn(list, [symbol = "$"],[precision = 2],[thousand = ","],[decimal = "."],[format = "%s%v"])
    -
    -// Second parameter can be an object (see formatNumber for available options):
    -accounting.formatColumn(list, [options])
    -
    -// Example usage (NB. use a space after the symbol to add arbitrary padding to all values):
    -var list = [123, 12345];
    -accounting.formatColumn(list, "$ ", 0); // ["$    123", "$ 12,345"]
    -
    -// List of numbers can be a multi-dimensional array (formatColumn is applied recursively):
    -var list = [[1, 100], [900, 9]];
    -accounting.formatColumn(list); // [["$  1.00", "$100.00"], ["$900.00", "$  9.00"]]
    - - -

    accounting.formatNumber()

    -
    // Standard usage and parameters (returns string):
    -accounting.formatNumber(number, [precision = 0], [thousand = ","], [decimal = "."])
    -
    -// Second parameter can also be an object matching `settings.number`:
    -accounting.formatNumber(number, [object])
    -
    -// Example usage:
    -accounting.formatNumber(9876543); // 9,876,543
    -accounting.formatNumber(4999.99, 2, ".", ","); // 4.999,99
    -
    -// Example usage with options object:
    -accounting.formatNumber(5318008, {
    -	precision : 3,
    -	thousand : " "
    -});
    -
    -// Will recursively format an array of values:
    -accounting.formatNumber([123456, [7890, 123]]); // ["123,456", ["7,890", "123"]]
    - - -

    accounting.toFixed()

    -
    // Standard usage and parameters (returns string):
    -accounting.toFixed(number, [precision = 0]);
    -
    -// Example usage:
    -accounting.toFixed(0.615, 2); // "0.62"
    -
    -// Compare to regular JavaScript `Number.toFixed()` method:
    -(0.615).toFixed(2); // "0.61"
    - - -

    accounting.unformat()

    -
    // Standard usage and parameters (returns number):
    -accounting.unformat(string, [decimal]);
    -
    -// Example usage:
    -accounting.unformat("GBP £ 12,345,678.90"); // 12345678.9
    -
    -// If a non-standard decimal separator was used (eg. a comma) unformat() will need it in order to work out
    -// which part of the number is a decimal/float:
    -accounting.unformat("€ 1.000.000,00", ","); // 1000000
    - -
    - - -
    -

    Roadmap

    -

    See the Github Issues page for up-to-date progress.

    -

    Next Version:

    -
      -
    • Add more fine-grained control of formatting, with negatives and zero-values
    • -
    • Implement map() and type-checking helper methods to clean up API methods
    • -
    • Find performance bottlenecks and work on speed optimisations
    • -
    • Write more tests, docs and examples, add FAQ
    • -
    • Implement feedback
    • -
    -

    Later:

    -
      -
    • Add padding parameter to override amount of space between currency symbol and value.
    • -
    • Add digit-grouping control, to allow eg. "$10,0000"
    • -
    • Add choice of rounding method for precision (up, down or nearest-neighbour).
    • -
    • Add several other general and excel-style money formatting methods.
    • -
    • Create NPM package, if there's demand for it.
    • -
    • Create wrapper for jQuery as a separate plugin (not in core) to allow eg. $('td.accounting').formatMoney()
    • -
    -
    - -
    -

    Feedback / Support

    -

    Please create issues on the accounting.js Github repository if you have feedback or need support, or comment on Joss's blog.

    -
    - -
    -

    Download

    - -
    - -
    -
    -
    -

     

    -

    Moar Info

    -

    accounting.js was made by Joss (that's me) in Hong Kong, for internal use in our enterprise analytics application at Demand Analytics. It seemed to be working pretty well, so I'm releasing it in the hope that other people and apps can make use of and contribute to it.

    -

    You can follow me on Twitter at @josscrowcroft, I mostly post about JS, HTML5, startups, Hong Kong and things I've built.

    -

    Any questions? Just email me here or post on the accounting.js Github repository.

    -
    - - - - - - - - - \ No newline at end of file diff --git a/readme.md b/readme.md deleted file mode 100644 index 43f14df..0000000 --- a/readme.md +++ /dev/null @@ -1,3 +0,0 @@ -*accounting.js* is a tiny JavaScript library for number money and currency formatting, with optional excel-style column rendering (to line up symbols and decimals). It's lightweight, fully localisable and has zero dependencies. - -This branch contains the pages for the github site at **http://josscrowcroft.github.com/accounting.js/**. \ No newline at end of file diff --git a/style.css b/style.css deleted file mode 100644 index 3bca986..0000000 --- a/style.css +++ /dev/null @@ -1,186 +0,0 @@ -/* Borrowed from Markdown.css: http://kevinburke.bitbucket.org/markdowncss/markdown.css */ -body{ - margin: 0 auto; - font-family: Georgia, Palatino, serif; - color: #444444; - line-height: 1; - max-width: 960px; - padding: 30px; -} -h1, h2, h3, h4 { - color: #111111; - font-weight: 400; -} -h1, h2, h3, h4, h5, p, ul, ol { - margin-bottom: 24px; - padding: 0; -} -h1 { - font-size: 48px; -} -h2 { - font-size: 36px; - margin: 24px 0 6px; -} -h3 { - font-size: 24px; -} -h4 { - font-size: 21px; -} -h5 { - font-size: 18px; -} -a { - color: #0099ff; - margin: 0; - padding: 0; - vertical-align: baseline; -} -li { - line-height: 24px; -} -li ul, li ul { - margin-left: 24px; -} -p, ul, ol { - font-size: 16px; - line-height: 24px; - max-width: 540px; -} -pre { - padding: 0px 24px; - max-width: 800px; - white-space: pre-wrap; -} -code { - font-family: Consolas, Monaco, Andale Mono, monospace; - line-height: 1.5; - font-size: 13px; -} -aside { - display: block; - float: right; - width: 390px; -} -blockquote { - margin: 1em 2em; - max-width: 476px; -} -blockquote p { - color: #666; - max-width: 460px; -} -hr { - margin: 0; - padding:0; - border:0; - outline:0; - height:2px; - background: #bbb; -} - - -/* Some overrides: */ -/* Thx to http://twitter.github.com/bootstrap/ */ - -h1 { font-size:40px; } -h2 { font-size:30px; } -h3 { font-size:22px; } -h4 { font-size:18px; padding-top:10px;} -h5 { font-size:1em; margin-bottom:1em; } -p, ul, ol { max-width: 580px; } -ol, ul { padding-left:24px; font-size:0.85em } -ol#list { font-size:1em } -a:hover, a:focus { outline:none; color:#e25525 } - -section { - padding:0 0 10px; -} -table{width:100%;margin-bottom:18px;padding:0;text-align:left;border-collapse:separate;font-size:13px;}table th,table td{padding:10px 10px 9px;line-height:13.5px;vertical-align:middle;border-bottom:1px solid #ddd;} -table th{padding-top:9px;font-weight:bold;border-bottom-width:2px;} - -td { - white-space:pre; - font-family:monospace; -} - -pre { - background-color: whiteSmoke; - display: block; - padding: 17px; - margin: 0 0 22px; - line-height: 18px; - border: 1px solid rgba(0, 0, 0, 0.15); - -webkit-border-radius: 3px; - -moz-border-radius: 3px; - border-radius: 3px; - white-space: pre-wrap; - overflow:hidden; -} -code, pre { - padding: 0 3px 2px; - font-family: Consolas, Monaco, "Andale Mono", "Courier New", monospace; - font-size: 13px; - -webkit-border-radius: 3px; - -moz-border-radius: 3px; - border-radius: 3px; -} - -pre.prettyprint { - background: #fefbf3; - padding: 9px; - border: 1px solid rgba(0,0,0,.2); - -webkit-box-shadow: 0 1px 2px rgba(0,0,0,.1); - -moz-box-shadow: 0 1px 2px rgba(0,0,0,.1); - box-shadow: 0 1px 2px rgba(0,0,0,.1); -} - -.pln{color:#222} -@media screen{ - .str{color:#080} - .kwd{color:#008} - .com{color:#800} - .typ{color:#606} - .lit{color:#066} - .pun, - .opn, - .clo{color:#660} - .tag{color:#008} - .atn{color:#606} - .atv{color:#080} - .dec, - .var{color:#606} - .fun{color:red} -} -@media print,projection{ - .str{color:#060} - .kwd{color:#006;font-weight:bold} - .com{color:#600;font-style:italic} - .typ{color:#404;font-weight:bold} - .lit{color:#044} - .pun, - .opn, - .clo{color:#440} - .tag{color:#006;font-weight:bold} - .atn{color:#404} - .atv{color:#060} -} - - -div.well { - background: whiteSmoke; - margin: 20px 0; - padding: 5px 12px; - min-height: 20px; - border: 1px solid #DDD; - -webkit-border-radius: 4px; - -moz-border-radius: 4px; - border-radius: 4px; - -webkit-box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.05); - -moz-box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.05); - box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.05); -} -.well p { - max-width:100%; -} \ No newline at end of file From bac3c789edca1c3ddf299f2ffe1037ebb38b3c65 Mon Sep 17 00:00:00 2001 From: euforic Date: Wed, 26 Oct 2011 10:44:14 -0700 Subject: [PATCH 27/58] Added patch to allow for module use with Titanium Appcelerator --- accounting.js | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/accounting.js b/accounting.js index 41295f8..820021a 100644 --- a/accounting.js +++ b/accounting.js @@ -375,9 +375,12 @@ // Export accounting for CommonJS. If being loaded as an AMD module, define it as such. // Otherwise, just add `accounting` to the global object - if (typeof module !== 'undefined' && module.exports) { - module.exports = lib; - lib.accounting = lib; + if (typeof exports !== 'undefined') { + if (typeof module !== 'undefined' && module.exports) { + module.exports = lib; + lib.accounting = lib; + } + exports.accounting = lib; } else if (typeof define === 'function' && define.amd) { // Return the library as an AMD module: define([], function() { From 969d942b0e5784ce83091021cedb6570885d32c4 Mon Sep 17 00:00:00 2001 From: Joss Crowcroft Date: Fri, 9 Dec 2011 23:49:17 +0800 Subject: [PATCH 28/58] simplified module definition a little based on underscore.js --- accounting.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/accounting.js b/accounting.js index 820021a..9e90a33 100644 --- a/accounting.js +++ b/accounting.js @@ -377,8 +377,7 @@ // Otherwise, just add `accounting` to the global object if (typeof exports !== 'undefined') { if (typeof module !== 'undefined' && module.exports) { - module.exports = lib; - lib.accounting = lib; + exports = module.exports = lib; } exports.accounting = lib; } else if (typeof define === 'function' && define.amd) { From 46443d1c7c16bc1342ca0be0a8567c89fd8e5426 Mon Sep 17 00:00:00 2001 From: Joss Crowcroft Date: Sat, 10 Dec 2011 01:57:09 +0800 Subject: [PATCH 29/58] update license to MIT. go nuts\! --- accounting.js | 11 +++++++---- accounting.min.js | 4 +++- license | 22 ++++++++++++++++++++++ package.json | 2 +- 4 files changed, 33 insertions(+), 6 deletions(-) create mode 100644 license diff --git a/accounting.js b/accounting.js index 9e90a33..eb96a52 100644 --- a/accounting.js +++ b/accounting.js @@ -1,9 +1,12 @@ /*! - * accounting.js javascript library v0.3.0 - * http://josscrowcroft.github.com/accounting.js/ + * accounting.js v0.3.1 + * Copyright 2011, Joss Crowcroft + * + * Freely distributable under the MIT license. + * Portions of accounting.js are inspired or borrowed from underscore.js * - * Copyright 2011 by Joss Crowcroft - * Licensed under GPL v3 | http://www.gnu.org/licenses/gpl-3.0.txt + * Full details and documentation: + * http://josscrowcroft.github.com/accounting.js/ */ (function(root, undefined) { diff --git a/accounting.min.js b/accounting.min.js index a4e1a1d..e10ae80 100644 --- a/accounting.min.js +++ b/accounting.min.js @@ -1,2 +1,4 @@ -/* accounting.js v0.3.0 - http://josscrowcroft.github.com/accounting.js */ +/*! + * accounting.js v0.3.1, copyright 2011 Joss Crowcroft, MIT license, http://josscrowcroft.github.com/accounting.js + */ (function(p,z){function q(a){return!!(a===""||a&&a.charCodeAt&&a.substr)}function m(a){return u?u(a):v.call(a)==="[object Array]"}function r(a){return v.call(a)==="[object Object]"}function s(a,b){var d,a=a||{},b=b||{};for(d in b)b.hasOwnProperty(d)&&a[d]==null&&(a[d]=b[d]);return a}function j(a,b,d){var c=[],e,h;if(!a)return c;if(w&&a.map===w)return a.map(b,d);for(e=0,h=a.length;e3?g.length%3:0;return f+(l?g.substr(0,l)+e.thousand:"")+g.substr(l).replace(/(\d{3})(?=\d)/g,"$1"+e.thousand)+(h?e.decimal+y(Math.abs(a),h).split(".")[1]:"")},A=c.formatMoney=function(a,b,d,i,e,h){if(m(a))return j(a,function(a){return A(a,b,d,i,e,h)});var a=o(a),f=s(r(b)?b:{symbol:b,precision:d,thousand:i,decimal:e,format:h},c.settings.currency),g=x(f.format);return(a>0?g.pos:a<0?g.neg:g.zero).replace("%s",f.symbol).replace("%v",t(Math.abs(a),n(f.precision),f.thousand,f.decimal))};c.formatColumn=function(a,b,d,i,e,h){if(!a)return[];var f=s(r(b)?b:{symbol:b,precision:d,thousand:i,decimal:e,format:h},c.settings.currency),g=x(f.format),l=g.pos.indexOf("%s")0?g.pos:a<0?g.neg:g.zero).replace("%s",f.symbol).replace("%v",t(Math.abs(a),n(f.precision),f.thousand,f.decimal));if(a.length>k)k=a.length;return a}});return j(a,function(a){return q(a)&&a.length Date: Sat, 10 Dec 2011 02:01:05 +0800 Subject: [PATCH 30/58] re-build and bump version number --- accounting.js | 2 +- accounting.min.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/accounting.js b/accounting.js index eb96a52..178fea6 100644 --- a/accounting.js +++ b/accounting.js @@ -17,7 +17,7 @@ var lib = {}; // Current version - lib.version = '0.3.0'; + lib.version = '0.3.1'; /* --- Exposed settings --- */ diff --git a/accounting.min.js b/accounting.min.js index e10ae80..643b899 100644 --- a/accounting.min.js +++ b/accounting.min.js @@ -1,4 +1,4 @@ /*! * accounting.js v0.3.1, copyright 2011 Joss Crowcroft, MIT license, http://josscrowcroft.github.com/accounting.js */ -(function(p,z){function q(a){return!!(a===""||a&&a.charCodeAt&&a.substr)}function m(a){return u?u(a):v.call(a)==="[object Array]"}function r(a){return v.call(a)==="[object Object]"}function s(a,b){var d,a=a||{},b=b||{};for(d in b)b.hasOwnProperty(d)&&a[d]==null&&(a[d]=b[d]);return a}function j(a,b,d){var c=[],e,h;if(!a)return c;if(w&&a.map===w)return a.map(b,d);for(e=0,h=a.length;e3?g.length%3:0;return f+(l?g.substr(0,l)+e.thousand:"")+g.substr(l).replace(/(\d{3})(?=\d)/g,"$1"+e.thousand)+(h?e.decimal+y(Math.abs(a),h).split(".")[1]:"")},A=c.formatMoney=function(a,b,d,i,e,h){if(m(a))return j(a,function(a){return A(a,b,d,i,e,h)});var a=o(a),f=s(r(b)?b:{symbol:b,precision:d,thousand:i,decimal:e,format:h},c.settings.currency),g=x(f.format);return(a>0?g.pos:a<0?g.neg:g.zero).replace("%s",f.symbol).replace("%v",t(Math.abs(a),n(f.precision),f.thousand,f.decimal))};c.formatColumn=function(a,b,d,i,e,h){if(!a)return[];var f=s(r(b)?b:{symbol:b,precision:d,thousand:i,decimal:e,format:h},c.settings.currency),g=x(f.format),l=g.pos.indexOf("%s")0?g.pos:a<0?g.neg:g.zero).replace("%s",f.symbol).replace("%v",t(Math.abs(a),n(f.precision),f.thousand,f.decimal));if(a.length>k)k=a.length;return a}});return j(a,function(a){return q(a)&&a.lengtha?"-":"",g=parseInt(y(Math.abs(a||0),h),10)+"",l=3a?g.neg:g.zero).replace("%s",f.symbol).replace("%v",t(Math.abs(a),n(f.precision),f.thousand,f.decimal))};c.formatColumn=function(a,b,d,i,e,h){if(!a)return[];var f=s(r(b)?b:{symbol:b,precision:d,thousand:i,decimal:e,format:h},c.settings.currency),g=x(f.format),l=g.pos.indexOf("%s")a?g.neg:g.zero).replace("%s",f.symbol).replace("%v",t(Math.abs(a),n(f.precision),f.thousand,f.decimal));if(a.length>k)k=a.length;return a});return j(a,function(a){return q(a)&&a.length Date: Thu, 26 Jan 2012 19:12:57 +0100 Subject: [PATCH 31/58] changed usage to reflect actual parameter order --- accounting.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/accounting.js b/accounting.js index 178fea6..9743fa5 100644 --- a/accounting.js +++ b/accounting.js @@ -264,8 +264,8 @@ /** * Format a number into currency * - * Usage: accounting.formatMoney(number, precision, symbol, thousandsSep, decimalSep, format) - * defaults: (0, 2, "$", ",", ".", "%s%v") + * 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 * Second param can be an object matching `settings.currency` which is the easiest way. From 447802f91dcf5c410abb9ac6316d64143f571037 Mon Sep 17 00:00:00 2001 From: Joss Crowcroft Date: Sat, 28 Jan 2012 10:07:44 +0800 Subject: [PATCH 32/58] package.json dependencies should be an object --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 57bd995..ca5364b 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,7 @@ "keywords" : ["accounting", "number", "money", "currency", "format", "utilities"], "author": "Joss Crowcroft (http://www.josscrowcroft.com)", "contributors" : [], - "dependencies" : [], + "dependencies" : {}, "repository" : {"type": "git", "url": "git://github.com/josscrowcroft/accounting.js.git"}, "main" : "accounting.js", "version" : "0.3.1" From 217955719654435dc241bee3af90b472d10c080b Mon Sep 17 00:00:00 2001 From: Joss Crowcroft Date: Sat, 28 Jan 2012 10:53:00 +0800 Subject: [PATCH 33/58] bump version number --- accounting.js | 4 ++-- accounting.min.js | 4 ++-- package.json | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/accounting.js b/accounting.js index 9743fa5..6af7048 100644 --- a/accounting.js +++ b/accounting.js @@ -1,5 +1,5 @@ /*! - * accounting.js v0.3.1 + * accounting.js v0.3.2 * Copyright 2011, Joss Crowcroft * * Freely distributable under the MIT license. @@ -17,7 +17,7 @@ var lib = {}; // Current version - lib.version = '0.3.1'; + lib.version = '0.3.2'; /* --- Exposed settings --- */ diff --git a/accounting.min.js b/accounting.min.js index 643b899..0fc755d 100644 --- a/accounting.min.js +++ b/accounting.min.js @@ -1,4 +1,4 @@ /*! - * accounting.js v0.3.1, copyright 2011 Joss Crowcroft, MIT license, http://josscrowcroft.github.com/accounting.js + * accounting.js v0.3.2, copyright 2011 Joss Crowcroft, MIT license, http://josscrowcroft.github.com/accounting.js */ -(function(p,z){function q(a){return!!(""===a||a&&a.charCodeAt&&a.substr)}function m(a){return u?u(a):"[object Array]"===v.call(a)}function r(a){return"[object Object]"===v.call(a)}function s(a,b){var d,a=a||{},b=b||{};for(d in b)b.hasOwnProperty(d)&&null==a[d]&&(a[d]=b[d]);return a}function j(a,b,d){var c=[],e,h;if(!a)return c;if(w&&a.map===w)return a.map(b,d);for(e=0,h=a.length;ea?"-":"",g=parseInt(y(Math.abs(a||0),h),10)+"",l=3a?g.neg:g.zero).replace("%s",f.symbol).replace("%v",t(Math.abs(a),n(f.precision),f.thousand,f.decimal))};c.formatColumn=function(a,b,d,i,e,h){if(!a)return[];var f=s(r(b)?b:{symbol:b,precision:d,thousand:i,decimal:e,format:h},c.settings.currency),g=x(f.format),l=g.pos.indexOf("%s")a?g.neg:g.zero).replace("%s",f.symbol).replace("%v",t(Math.abs(a),n(f.precision),f.thousand,f.decimal));if(a.length>k)k=a.length;return a});return j(a,function(a){return q(a)&&a.lengtha?"-":"",g=parseInt(y(Math.abs(a||0),h),10)+"",l=3a?g.neg:g.zero).replace("%s",f.symbol).replace("%v",t(Math.abs(a),n(f.precision),f.thousand,f.decimal))};c.formatColumn=function(a,b,d,i,e,h){if(!a)return[];var f=s(r(b)?b:{symbol:b,precision:d,thousand:i,decimal:e,format:h},c.settings.currency),g=x(f.format),l=g.pos.indexOf("%s")a?g.neg:g.zero).replace("%s",f.symbol).replace("%v",t(Math.abs(a),n(f.precision),f.thousand,f.decimal));if(a.length>k)k=a.length;return a});return j(a,function(a){return q(a)&&a.length (http://www.josscrowcroft.com)", @@ -8,5 +8,5 @@ "dependencies" : {}, "repository" : {"type": "git", "url": "git://github.com/josscrowcroft/accounting.js.git"}, "main" : "accounting.js", - "version" : "0.3.1" + "version" : "0.3.2" } \ No newline at end of file From 335d3662c1378a92a43a038a9eea9d46961d46a5 Mon Sep 17 00:00:00 2001 From: Joss Crowcroft Date: Sat, 28 Jan 2012 10:53:16 +0800 Subject: [PATCH 34/58] overhaul readme for 0.3.2 --- readme.md | 76 +++++++++++++++++++++++++++---------------------------- 1 file changed, 38 insertions(+), 38 deletions(-) diff --git a/readme.md b/readme.md index aa93ff6..2e25b29 100644 --- a/readme.md +++ b/readme.md @@ -1,65 +1,65 @@ -**accounting.js** is a tiny JavaScript library for number, money and currency formatting, and excel-style column rendering to line up symbols and decimals. It's lightweight, fully localisable and has zero dependencies. +**accounting.js** is a tiny JavaScript library for number, money and currency parsing/formatting. It's lightweight and fully localisable, with zero dependencies, and works great as a nodeJS/npm and AMD/requireJS module. -Check out the plugin homepage (with demos and docs) here: **http://josscrowcroft.github.com/accounting.js/** +Check out the **[accounting.js homepage](http://josscrowcroft.github.com/accounting.js/)** for demos and documentation! -Please checkout / download the latest stable tag before using in a production environment and report any bugs with your setup info! +Please checkout/download the latest stable tag before using in production. [Bug reports](https://github.com/josscrowcroft/accounting.js/issues) and pull requests hugely welcome. +Made with love by [@josscrowcroft](http://twitter.com/josscrowcroft) and some excellent [contributors](https://github.com/josscrowcroft/accounting.js/contributors). -## Roadmap / to-do +--- + +### Also try: -* Write some more tests, docs and demos -* Do some optimisations for performance bottlenecks +* **[money.js](http://josscrowcroft.github.com/money.js)** - a tiny (1kb) javascript currency conversion library, for web & nodeJS +* **[open exchange rates](http://josscrowcroft.github.com/open-exchange-rates/)** - free / open source hourly-updated currency conversion data for everybody +--- ## Changelog -### v0.3.0 -* Rewrote library structure similar to underscore.js for use as a nodeJS/npm and AMD module - now you can go `npm install accounting`, and then `var accounting = require("accounting");` in your nodeJS scripts. -* It also works with requireJS or any AMD module loader. -* **unformat** now only attempts to parse the number if it's not already a valid number (to save on processing). It's also aliased as `acounting.parse` +**v0.3.2** - Fix package.json dependencies (should be empty object) and tweak comments + +**v0.3.0** + +* Rewrote library structure similar to underscore.js for use as a **nodeJS/npm** and **AMD/requireJS** module - now you can go `npm install accounting`, and then `var accounting = require("accounting");` in your nodeJS scripts. +* **unformat** now only attempts to parse the number if it's not already a valid number. Also now aliased as `acounting.parse` * Fixed an IE bug in the `defaults` method -* Numerous other bugfixes and little improvements -### v0.2.2 -* Fixed same issue as #24 in **formatNumber** -* accounting.min.js now uses Google Closure Compiler, shaves 0.3kb+ off previous size -* Merged branch `gh-pages` and `master` and reshuffled to save hassle +**v0.2.2** - Fixed same issue as \#Num: #24 in **formatNumber**; switch to Google Closure Compiler for minified version. -### v0.2.1 -* Fixed issue #24 where locally-defined settings object was being modified by **formatMoney** (still an issue in **formatNumber**, to fix) +**v0.2.1** - Fixed issue \#Num: #24 where locally-defined settings object was being modified by **formatMoney** (still an issue in **formatNumber**) -### v0.2 -* Compleat (non-breaking) rewrite of the formatting system for **formatMoney** and **formatColumn**, to allow more fine-grained control of string output, still easy as pie +**v0.2** + +* Rewrote formatting system for **formatMoney** and **formatColumn**, to allow easier/better control of string output * Separate formats for negative and zero values now supported (optionally) via `accounting.settings.currency.format` -* Added `isArray`, `isString` and `isObject` helper methods from underscore.js for readability/maintainability +* Internal improvements and helper methods -### v0.1.5 -* Added `map()` helper method based on underscore.js's `_.map()`, for array iteration, falls back to native `Array.map` if available. +**v0.1.4** - **formatMoney** recursively formats arrays; added Jasmine test suite (thx [millermedeiros](https://github.com/millermedeiros)!) and QUnit functionality/speed tests -### v0.1.4 -* Added helper method `defaults` to extend an object's empty properties with a defaults object -* API methods now use **defaults()** to give more structure to the second-parameter-object method style: parameters are normalised into an `opts` object inside each method -* Settings object at top of library for readibility -* **formatMoney** now recursively formats arrays -* Added Jasmine test suite (thx [millermedeiros](https://github.com/millermedeiros)!) and QUnit functionality + speed tests +**v0.1.3** -### v0.1.3 -* Refactored library codebase to use a configurable settings object for default formatting parameters, which paves the way for plugins later on. +* Added configurable settings object for default formatting parameters. * Added `format` parameter to control symbol and value position (default `"%s%v"`, or [symbol][value]) * Made methods more consistent in accepting an object as 2nd parameter, matching/overriding the library defaults -### v0.1.2 -* **formatColumn** now works recursively on nested arrays, eg `accounting.formatColumn( [[1,12,123,1234], [1234,123,12,1]] )`, returning back a matching array with inner columns lined up -* Another fix for rounding in **formatNumber**: `54324.535` now rounds to ".54" instead of ".53" +**v0.1.2** + +* **formatColumn** works recursively on nested arrays, eg `accounting.formatColumn( [[1,12,123,1234], [1234,123,12,1]] )`, returns matching array with inner columns lined up +* Another fix for rounding in **formatNumber**: `.535` now correctly rounds to ".54" instead of ".53" + +**v0.1.1** -### v0.1.1 * Added **toFixed** method (`accounting.toFixed(value, precision)`), which treats floats more like decimals for more accurate currency rounding - now, `0.615` rounds up to `$0.62` instead of `$0.61` -* Minified version now preserves semicolons -* Fixed NaN errors when no value in **unformat** - revisit later on +* Minified version preserves semicolons +* Fixed NaN errors when no value in **unformat** + +**v0.1** - First version yo + --- -### Still not convinced? Here's a little preview of accounting.formatColumn(): +### Here's a neat little preview of **formatColumn()**: ```html Original Number: | With accounting.js: | Different settings: | Symbol after value: @@ -72,4 +72,4 @@ Please checkout / download the latest stable tag before using in a production en 0 | $ 0.00 | HK$ -- | 0.00 GBP ``` -There's more on the plugin homepage: **http://josscrowcroft.github.com/accounting.js/** +There's more demos and documentation on the **[accounting.js homepage](http://josscrowcroft.github.com/accounting.js/)**. Enjoy! From 9ff4a4022e5c08f028d652d2b0ba1d4b65588bde Mon Sep 17 00:00:00 2001 From: Joss Crowcroft Date: Sat, 28 Jan 2012 11:05:27 +0800 Subject: [PATCH 35/58] merge changes from dev into master, leave experimental bits --- extensions/accounting-jquery.js | 77 --------------------------------- extensions/accounting-units.js | 49 --------------------- 2 files changed, 126 deletions(-) delete mode 100644 extensions/accounting-jquery.js delete mode 100644 extensions/accounting-units.js diff --git a/extensions/accounting-jquery.js b/extensions/accounting-jquery.js deleted file mode 100644 index 13f9717..0000000 --- a/extensions/accounting-jquery.js +++ /dev/null @@ -1,77 +0,0 @@ -/** - * accounting-jquery.js [alpha] - * - * jQuery plugin wrapper for accounting.js. Dependcies: jQuery, accounting.js (duh) - * Usage: `$(el).accounting("methodName", [opts]);` - */ - -(function($) { - var methods = { - settings: function(options) { - // Merge new options into accounting.settings - accounting.settings = $.extend(true, accounting.settings, options); - - // Return for chaining - return this; - }, - formatNumber: function(options) { - // Apply accounting.formatNumber() to matched elements and return for chaining - return this.each(function() { - $(this).text( - accounting.formatNumber($(this).text(), options) - ); - }); - }, - formatMoney: function(options) { - // Apply accounting.formatMoney() to matched elements and return for chaining - return this.each(function() { - $(this).text( - accounting.formatMoney($(this).text(), options) - ); - }); - }, - formatColumn: function(options) { - var column = []; - - // Collect our values into an array to pass to formatColumn() - this.each(function() { - column.push($(this).text()); - }); - - // Format the column of values - column = accounting.formatColumn(column, options); - - // Now set each of the elements' values and return for chaining - return this.each(function(i) { - $(this).text(column[i]); - }); - }, - toFixed: function(precision) { - // Apply accounting.toFixed() to matched elements and return for chaining - return this.each(function() { - $(this).text( - accounting.toFixed($(this).text(), precision) - ); - }); - }, - unformat: function(decimal) { - // Apply accounting.unformat() to matched elements and return for chaining - return this.each(function() { - $(this).text( - accounting.unformat($(this).text(), decimal) - ); - }); - } - }; - - $.fn.accounting = function(method) { - // Method calling logic - if (methods[method]) { - return methods[method].apply( this, Array.prototype.slice.call(arguments, 1)); - } else if (typeof method === 'object' || !method) { - return methods.settings.apply(this, arguments); - } else { - $.error('Method ' + method + ' does not exist on jQuery.accounting'); - } - }; -})(jQuery); diff --git a/extensions/accounting-units.js b/extensions/accounting-units.js deleted file mode 100644 index 86e5241..0000000 --- a/extensions/accounting-units.js +++ /dev/null @@ -1,49 +0,0 @@ -/** - * accounting-units.js [alpha] - * - * Adds a formatUnits method to the accounting.js library (load after accounting.js) - */ - -/** - * Format large values into units (eg. 100K, 12GB) - * - * `base` is the division between units (e.g. 1000 for thousands, 1024 for bytes, etc.) - * `units` is a string or array of strings to apply to each level of division - * (e.g. ["K", "M", "B"] for base 1000) - * `callback` is a function to apply to the value before returning, with the number and the - * relevant unit as params. Can be used to format the number/currency before adding the unit. - * - * Examples (see docs for more info): - * accounting.formatUnits(12345678, 1000, ["K", "M", "B"], function(num, units) { - * return accounting.formatNumber(num, 3) + units; - * }); // "12.346M" - * accounting.formatUnits(123456, 1000000, "M"); // only return millions - * accounting.formatUnits(12345678, 1024, ["KB", "MB", "GB"]); // bytes - */ -accounting.formatUnits = function(number, base, units, callback) { - // Clean up params: - number = accounting.unformat(number); - base = base || 1; - units = typeof units === "string" ? [units] : units || [""]; - - // These are used later: - var unit, - neg = number < 0; - - // If the number is smaller than base, divide it and add the first or only unit: - if ( number < base || base === 1 ) { - number /= base; - unit = units.shift(); - } else { - // Divide number by base until it's smaller than base or has reached the largest unit: - while ( base > 1 && number > Math.abs(base) && units.length ) { - number /= base; - - // Get next unit: - unit = units.shift(); - } - } - - // Apply callback(number, units) to number: - return typeof callback === "function" && callback.call([], number, unit) || number + unit; -}; From 1f22b43a916b1e786cfb30d80e1b62757cc19fc3 Mon Sep 17 00:00:00 2001 From: Francesco Montefoschi Date: Tue, 29 May 2012 21:44:00 +0200 Subject: [PATCH 36/58] unformat() uses by default the decimal separator provided in settings --- accounting.js | 7 ++++--- tests/qunit/methods.js | 5 +++++ 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/accounting.js b/accounting.js index 6af7048..cb02a88 100644 --- a/accounting.js +++ b/accounting.js @@ -168,8 +168,9 @@ * 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 (default: "."), so if the number - * uses a non-standard decimal separator, provide it as the second argument. + * Decimal must be included in the regular expression to match floats (defaults to + * accounting.settings.currency.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) * @@ -190,7 +191,7 @@ if (typeof value === "number") return value; // Default decimal point is "." but could be set to eg. "," in opts: - decimal = decimal || "."; + decimal = decimal || this.settings.currency.decimal; // Build regex to strip out everything except digits, decimal point and minus sign: var regex = new RegExp("[^0-9-" + decimal + "]", ["g"]), diff --git a/tests/qunit/methods.js b/tests/qunit/methods.js index 816580b..9600564 100644 --- a/tests/qunit/methods.js +++ b/tests/qunit/methods.js @@ -7,6 +7,11 @@ $(document).ready(function() { equals(accounting.unformat(1234567890), 1234567890, 'Returns same value when passed an integer'); equals(accounting.unformat("string"), 0, 'Returns 0 for a string with no numbers'); equals(accounting.unformat({joss:1}), 0, 'Returns 0 for object'); + + accounting.settings.currency.decimal = ','; + equals(accounting.unformat("100,00"), 100, 'Uses decimal separator from settings'); + equals(accounting.unformat("¤1.000,00"), 1000, 'Uses decimal separator from settings'); + accounting.settings.currency.decimal = '.'; }); test("accounting.toFixed()", function() { From 8f733a9612d835a409a1b184a0732a93f387eb08 Mon Sep 17 00:00:00 2001 From: Keith Pitt Date: Wed, 30 May 2012 17:16:21 +1000 Subject: [PATCH 37/58] Fixed isObject for Internet Explorer 7 toString.call(undefined) or toString.call(null) will return "[object Object] in IE7 and 8. --- accounting.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/accounting.js b/accounting.js index 6af7048..5693793 100644 --- a/accounting.js +++ b/accounting.js @@ -69,7 +69,7 @@ * Tests whether supplied parameter is a true object */ function isObject(obj) { - return toString.call(obj) === '[object Object]'; + return obj && toString.call(obj) === '[object Object]'; } /** From 0266259964fe5196d19049f929b00e1e1dbf5327 Mon Sep 17 00:00:00 2001 From: Francesco Montefoschi Date: Fri, 1 Jun 2012 00:49:47 +0200 Subject: [PATCH 38/58] number decimal separator instead of currency separator --- accounting.js | 6 +++--- tests/qunit/methods.js | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/accounting.js b/accounting.js index cb02a88..d41cdac 100644 --- a/accounting.js +++ b/accounting.js @@ -169,7 +169,7 @@ * alias: accounting.`parse(string)` * * Decimal must be included in the regular expression to match floats (defaults to - * accounting.settings.currency.decimal), so if the number uses a non-standard decimal + * accounting.settings.number.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) @@ -190,8 +190,8 @@ // Return the value as-is if it's already a number: if (typeof value === "number") return value; - // Default decimal point is "." but could be set to eg. "," in opts: - decimal = decimal || this.settings.currency.decimal; + // Default decimal point comes from settings, but could be set to eg. "," in opts: + decimal = decimal || this.settings.number.decimal; // Build regex to strip out everything except digits, decimal point and minus sign: var regex = new RegExp("[^0-9-" + decimal + "]", ["g"]), diff --git a/tests/qunit/methods.js b/tests/qunit/methods.js index 9600564..19e354b 100644 --- a/tests/qunit/methods.js +++ b/tests/qunit/methods.js @@ -8,10 +8,10 @@ $(document).ready(function() { equals(accounting.unformat("string"), 0, 'Returns 0 for a string with no numbers'); equals(accounting.unformat({joss:1}), 0, 'Returns 0 for object'); - accounting.settings.currency.decimal = ','; + accounting.settings.number.decimal = ','; equals(accounting.unformat("100,00"), 100, 'Uses decimal separator from settings'); equals(accounting.unformat("¤1.000,00"), 1000, 'Uses decimal separator from settings'); - accounting.settings.currency.decimal = '.'; + accounting.settings.number.decimal = '.'; }); test("accounting.toFixed()", function() { From 1c3ffe8ae280bb9b7a574cf87c2cf9cea1d93722 Mon Sep 17 00:00:00 2001 From: Ryan Wood Date: Sat, 9 Jun 2012 15:16:00 -0400 Subject: [PATCH 39/58] fix reference to settings --- accounting.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/accounting.js b/accounting.js index 955a098..d9e12eb 100644 --- a/accounting.js +++ b/accounting.js @@ -191,7 +191,7 @@ if (typeof value === "number") return value; // Default decimal point comes from settings, but could be set to eg. "," in opts: - decimal = decimal || this.settings.number.decimal; + decimal = decimal || lib.settings.number.decimal; // Build regex to strip out everything except digits, decimal point and minus sign: var regex = new RegExp("[^0-9-" + decimal + "]", ["g"]), From 4a95cc7e14ba74fa581ca9182e9b476e9c052ad2 Mon Sep 17 00:00:00 2001 From: Joss Crowcroft Date: Mon, 14 Jul 2014 20:44:03 +0100 Subject: [PATCH 40/58] v0.4 updates --- accounting.js | 6 +++--- accounting.min.js | 2 +- index.html | 25 ++++++++++++------------- license | 2 +- package.json | 8 ++++---- readme.md | 7 +++++-- 6 files changed, 26 insertions(+), 24 deletions(-) diff --git a/accounting.js b/accounting.js index 178fea6..998cfed 100644 --- a/accounting.js +++ b/accounting.js @@ -1,12 +1,12 @@ /*! - * accounting.js v0.3.1 - * Copyright 2011, Joss Crowcroft + * accounting.js v0.4 + * Copyright 2014 Open Exchange Rates * * Freely distributable under the MIT license. * Portions of accounting.js are inspired or borrowed from underscore.js * * Full details and documentation: - * http://josscrowcroft.github.com/accounting.js/ + * http://openexchangerates.github.io/accounting.js/ */ (function(root, undefined) { diff --git a/accounting.min.js b/accounting.min.js index 643b899..b760dfa 100644 --- a/accounting.min.js +++ b/accounting.min.js @@ -1,4 +1,4 @@ /*! - * accounting.js v0.3.1, copyright 2011 Joss Crowcroft, MIT license, http://josscrowcroft.github.com/accounting.js + * accounting.js v0.4, copyright 2014 Open Exchange Rates, MIT license, http://openexchangerates.github.io/accounting.js */ (function(p,z){function q(a){return!!(""===a||a&&a.charCodeAt&&a.substr)}function m(a){return u?u(a):"[object Array]"===v.call(a)}function r(a){return"[object Object]"===v.call(a)}function s(a,b){var d,a=a||{},b=b||{};for(d in b)b.hasOwnProperty(d)&&null==a[d]&&(a[d]=b[d]);return a}function j(a,b,d){var c=[],e,h;if(!a)return c;if(w&&a.map===w)return a.map(b,d);for(e=0,h=a.length;ea?"-":"",g=parseInt(y(Math.abs(a||0),h),10)+"",l=3a?g.neg:g.zero).replace("%s",f.symbol).replace("%v",t(Math.abs(a),n(f.precision),f.thousand,f.decimal))};c.formatColumn=function(a,b,d,i,e,h){if(!a)return[];var f=s(r(b)?b:{symbol:b,precision:d,thousand:i,decimal:e,format:h},c.settings.currency),g=x(f.format),l=g.pos.indexOf("%s")a?g.neg:g.zero).replace("%s",f.symbol).replace("%v",t(Math.abs(a),n(f.precision),f.thousand,f.decimal));if(a.length>k)k=a.length;return a});return j(a,function(a){return q(a)&&a.length accounting.js - format money / currency in JavaScript - + + + From ab30b96226dab5aabecacab0ba588a4a65783113 Mon Sep 17 00:00:00 2001 From: James Squillante Date: Fri, 27 Jan 2017 18:39:28 -0600 Subject: [PATCH 55/58] update regex to get all unformat jasmine tests to pass. --- accounting.js | 2 +- tests/jasmine/core/unformatSpec.js | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/accounting.js b/accounting.js index 42c6bb2..698b30a 100644 --- a/accounting.js +++ b/accounting.js @@ -197,7 +197,7 @@ var regex = new RegExp("[^0-9-" + decimal + "]", ["g"]), unformatted = parseFloat( ("" + value) - .replace(/\((.*)\)/, "-$1") // replace bracketed values with negatives + .replace(/\((?=\d+)(.*)\)/, "-$1") // replace bracketed values with negatives .replace(regex, '') // strip out any cruft .replace(decimal, '.') // make sure decimal point is standard ); diff --git a/tests/jasmine/core/unformatSpec.js b/tests/jasmine/core/unformatSpec.js index 1f0f17e..7c6a093 100644 --- a/tests/jasmine/core/unformatSpec.js +++ b/tests/jasmine/core/unformatSpec.js @@ -11,6 +11,7 @@ describe('unformat()', function(){ expect( accounting.unformat('$ -123,456') ).toBe( -123456 ); expect( accounting.unformat('$ -123,456.78') ).toBe( -123456.78 ); expect( accounting.unformat('&*()$ -123,456') ).toBe( -123456 ); + expect( accounting.unformat('&*()$(123,456)A$@P') ).toBe( -123456 ); expect( accounting.unformat(';$@#$%^&-123,456.78') ).toBe( -123456.78 ); }); From 9d65e654a7cd4db0248bbeb99388642b3517f8fb Mon Sep 17 00:00:00 2001 From: Gustav Larsson Date: Wed, 8 Mar 2017 17:30:03 +0100 Subject: [PATCH 56/58] Updated toFixed method to handle all cases --- accounting.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/accounting.js b/accounting.js index 698b30a..fd181fd 100644 --- a/accounting.js +++ b/accounting.js @@ -215,10 +215,11 @@ */ var toFixed = lib.toFixed = function(value, precision) { precision = checkPrecision(precision, lib.settings.number.precision); - var power = Math.pow(10, precision); - // Multiply up by precision, round accurately, then divide and use native toFixed(): - return (Math.round(lib.unformat(value) * power) / power).toFixed(precision); + var exponentialForm = Number(lib.unformat(value) + 'e' + precision); + var rounded = Math.round(exponentialForm); + var finalResult = Number(rounded + 'e-' + precision).toFixed(precision); + return finalResult; }; From 7a8b659c245720e79814a6fdae85f8fc4f0e052a Mon Sep 17 00:00:00 2001 From: Gustav Larsson Date: Wed, 8 Mar 2017 17:37:42 +0100 Subject: [PATCH 57/58] Added a test case for additional floating point error --- tests/jasmine/core/formatNumberSpec.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/jasmine/core/formatNumberSpec.js b/tests/jasmine/core/formatNumberSpec.js index 6ee6a51..46df133 100644 --- a/tests/jasmine/core/formatNumberSpec.js +++ b/tests/jasmine/core/formatNumberSpec.js @@ -13,10 +13,11 @@ describe('formatNumber', function(){ }); - it('should fix floting point rounding error', function(){ + it('should fix floating point rounding error', function(){ expect( accounting.formatNumber(0.615, 2) ).toBe( '0.62' ); expect( accounting.formatNumber(0.614, 2) ).toBe( '0.61' ); + expect( accounting.formatNumber(1.005, 2) ).toBe( '1.01'); }); From 09c6b348d7cb781e4fee920c3c9b6f1e13baa364 Mon Sep 17 00:00:00 2001 From: Ibrahim Date: Mon, 13 Nov 2017 15:54:20 +0000 Subject: [PATCH 58/58] Fix RegExp As per the ECMAScript, the flags in RegExp should simply be strings. The fact that the code still works is because RegExp applies toString on the flags. When there is only one element in the array, it works fine, but if there is another flag, e.g ["g", "i"], it will result in "g,i", which throws a SyntaxError. --- accounting.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/accounting.js b/accounting.js index fd181fd..2de6d7e 100644 --- a/accounting.js +++ b/accounting.js @@ -194,7 +194,7 @@ decimal = decimal || lib.settings.number.decimal; // Build regex to strip out everything except digits, decimal point and minus sign: - var regex = new RegExp("[^0-9-" + decimal + "]", ["g"]), + var regex = new RegExp("[^0-9-" + decimal + "]", "g"), unformatted = parseFloat( ("" + value) .replace(/\((?=\d+)(.*)\)/, "-$1") // replace bracketed values with negatives