diff --git a/accounting.js b/accounting.js index 755a3d5..b902b72 100644 --- a/accounting.js +++ b/accounting.js @@ -215,12 +215,39 @@ */ var toFixed = lib.toFixed = function(value, precision) { precision = checkPrecision(precision, lib.settings.number.precision); - var power = Math.pow(10, precision); + var unformattedValue = lib.unformat(value).toString(); - // Multiply up by precision, round accurately, then divide and use native toFixed(): - return (Math.round(lib.unformat(value) * power) / power).toFixed(precision); - }; + if(unformattedValue == 0) { + return Number(0).toFixed(precision); + } + + var number = unformattedValue.split("."); + var integer = number[0]; + + var mantissa = number.length > 1 ? number[1] : ""; + if(mantissa.length < precision) { + var diff = precision - mantissa.length; + for(var i=0; i 4) { + fullNumber += 1; + } + + var newValue = fullNumber.toString(); + + if(precision == 0) + return newValue; + + var dotIdx = newValue.length - precision; + newValue = (newValue.slice(0,dotIdx)||0) + '.' + (newValue.slice(dotIdx)||0); + return newValue; + }; /** * Format a number, with comma-separated thousands and custom precision/decimal places diff --git a/tests/jasmine/core/formatMoneySpec.js b/tests/jasmine/core/formatMoneySpec.js index cbfe12d..e3b6760 100644 --- a/tests/jasmine/core/formatMoneySpec.js +++ b/tests/jasmine/core/formatMoneySpec.js @@ -2,6 +2,7 @@ describe('formatMoney()', function(){ it('should work for small numbers', function(){ + expect( accounting.formatMoney(0) ).toBe( '$0.00' ); expect( accounting.formatMoney(123) ).toBe( '$123.00' ); expect( accounting.formatMoney(123.45) ).toBe( '$123.45' ); expect( accounting.formatMoney(12345.67) ).toBe( '$12,345.67' ); diff --git a/tests/jasmine/core/formatNumberSpec.js b/tests/jasmine/core/formatNumberSpec.js index 6ee6a51..a3c2340 100644 --- a/tests/jasmine/core/formatNumberSpec.js +++ b/tests/jasmine/core/formatNumberSpec.js @@ -3,7 +3,8 @@ describe('formatNumber', function(){ describe('rounding and enforce precision', function(){ it('should enforce precision and round values', function(){ - + + expect( accounting.formatNumber(0, 2) ).toBe( '0.00' ); expect( accounting.formatNumber(123.456789, 0) ).toBe( '123' ); expect( accounting.formatNumber(123.456789, 1) ).toBe( '123.5' ); expect( accounting.formatNumber(123.456789, 2) ).toBe( '123.46' ); @@ -11,12 +12,16 @@ describe('formatNumber', function(){ expect( accounting.formatNumber(123.456789, 4) ).toBe( '123.4568' ); expect( accounting.formatNumber(123.456789, 5) ).toBe( '123.45679' ); + expect( accounting.formatNumber(129.9, 0) ).toBe( '130' ); + expect( accounting.formatNumber(129.99, 1) ).toBe( '130.0' ); + }); - 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(8.325, 2) ).toBe( '8.33' ); });