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: 3 additions & 3 deletions accounting.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@
* Alias: `accounting.parse(string)`
*
* 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
* 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 @@ -194,10 +194,10 @@
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 + "Ee]", ["g"]),
unformatted = parseFloat(
("" + value)
.replace(/\((.*)\)/, "-$1") // replace bracketed values with negatives
.replace(/\((.+)\)/, "-$1") // replace bracketed values with negatives
.replace(regex, '') // strip out any cruft
.replace(decimal, '.') // make sure decimal point is standard
);
Expand Down
11 changes: 8 additions & 3 deletions tests/jasmine/core/unformatSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,32 @@ describe('unformat()', function(){
expect( accounting.unformat('$ 123,456.78') ).toBe( 123456.78 );
expect( accounting.unformat('&*()$ 123,456') ).toBe( 123456 );
expect( accounting.unformat(';$@#$%^&123,456.78') ).toBe( 123456.78 );
expect( accounting.unformat(';$@#$%^&123,456.78E-19') ).toBe( 123456.78E-19 );
});

it('should work with negative numbers', 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.78') ).toBe( -123456.78 );
expect( accounting.unformat(';$@#$%^&-123,456.78E-17') ).toBe( -123456.78E-17 );
});
it('should accept different decimal separators', function(){

it('should accept different decimal separators', function(){
expect( accounting.unformat('$ 123,456', ',') ).toBe( 123.456 );
expect( accounting.unformat('$ 123456|78', '|') ).toBe( 123456.78 );
expect( accounting.unformat('&*()$ 123>456', '>') ).toBe( 123.456 );
expect( accounting.unformat(';$@#$%^&123,456\'78', '\'') ).toBe( 123456.78 );
expect( accounting.unformat(';$@#$%^&123,456\'78E-17', '\'') ).toBe( 123456.78E-17 );
});

it('should accept an array', function(){
var vals = accounting.unformat(['$ 123', '$567.89', 'R$12,345,678.901']);
var vals = accounting.unformat(['$ 123', '$567.89', 'R$12,345,678.901', '3.21e-17']);
expect( vals[0] ).toBe( 123 );
expect( vals[1] ).toBe( 567.89 );
expect( vals[2] ).toBe( 12345678.901 );
expect( vals[3] ).toBe( 3.21e-17 );

});

});