Skip to content

Commit

Permalink
Merge pull request #43 from fmntf/master
Browse files Browse the repository at this point in the history
unformat() should use the default decimal separator via @fmntf
  • Loading branch information
Joss Crowcroft committed May 31, 2012
2 parents df6e8c9 + 0266259 commit d29a764
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
9 changes: 5 additions & 4 deletions accounting.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.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)
*
Expand All @@ -189,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 || ".";
// 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"]),
Expand Down
5 changes: 5 additions & 0 deletions tests/qunit/methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.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.number.decimal = '.';
});

test("accounting.toFixed()", function() {
Expand Down

0 comments on commit d29a764

Please sign in to comment.