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
8 changes: 5 additions & 3 deletions accounting.js
Original file line number Diff line number Diff line change
Expand Up @@ -216,9 +216,11 @@
var toFixed = lib.toFixed = function(value, precision) {
precision = checkPrecision(precision, lib.settings.number.precision);

var exponentialForm = Number(lib.unformat(value) + 'e' + precision);
var rounded = Math.round(exponentialForm);
var finalResult = Number(rounded + 'e-' + precision).toFixed(precision);
var shiftedExponentials = String(lib.unformat(value)).split('e');
var shiftedNumber = Number(shiftedExponentials[0] + 'e' + ((shiftedExponentials[1] ? Number(shiftedExponentials[1]) : 0) + precision));
var rounded = Math.round(shiftedNumber);
var roundedExponentials = String(rounded).split('e');
var finalResult = Number(roundedExponentials[0] + 'e' + ((roundedExponentials[1] ? Number(roundedExponentials[1]) : 0) - precision)).toFixed(precision);
return finalResult;
};

Expand Down
8 changes: 8 additions & 0 deletions tests/jasmine/core/formatNumberSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ describe('formatNumber', function(){

});

it('should format tiny numbers without returning NaN', function(){

expect( accounting.formatNumber(1e-7) ).toBe( '0' );
expect( accounting.formatNumber(0.0000001) ).toBe( '0' );
expect( accounting.formatNumber(1e-7, 8) ).toBe( '0.00000010' );

});

it('should work for large numbers', function(){

expect( accounting.formatNumber(123456.54321, 0) ).toBe( '123,457' );
Expand Down