Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions accounting.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,11 +176,11 @@
*
* Doesn't throw any errors (`NaN`s become 0) but this may change in future
*/
var unformat = lib.unformat = lib.parse = function(value, decimal) {
var unformat = lib.unformat = lib.parse = function(value, decimal, symbol) {
// Recursively unformat arrays:
if (isArray(value)) {
return map(value, function(val) {
return unformat(val, decimal);
return unformat(val, decimal, symbol);
});
}

Expand All @@ -192,12 +192,14 @@

// Default decimal point comes from settings, but could be set to eg. "," in opts:
decimal = decimal || lib.settings.number.decimal;
symbol = symbol || lib.settings.currency.symbol;

// Build regex to strip out everything except digits, decimal point and minus sign:
var regex = new RegExp("[^0-9-" + decimal + "]", ["g"]),
unformatted = parseFloat(
("" + value)
.replace(/\((?=\d+)(.*)\)/, "-$1") // replace bracketed values with negatives
.split(symbol).join("") // remove currency symbols before stripping other chars
.replace(regex, '') // strip out any cruft
.replace(decimal, '.') // make sure decimal point is standard
);
Expand Down
5 changes: 5 additions & 0 deletions tests/jasmine/core/unformatSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ describe('unformat()', function(){
expect( accounting.unformat(';$@#$%^&123,456\'78', '\'') ).toBe( 123456.78 );
});

it('should accept a currency symbol with the decimal separator', function(){
expect( accounting.unformat('kr. 123.45', '.', 'kr.') ).toBe( 123.45 );
expect( accounting.unformat(['kr. 123.45', 'kr. 67.89'], '.', 'kr.')[1] ).toBe( 67.89 );
});

it('should accept an array', function(){
var vals = accounting.unformat(['$ 123', '$567.89', 'R$12,345,678.901']);
expect( vals[0] ).toBe( 123 );
Expand Down