From 83e4f42b0a651a6556a8ff18a6a295d701b5c45b Mon Sep 17 00:00:00 2001 From: Mike Walters Date: Thu, 28 Jul 2016 13:45:52 -0400 Subject: [PATCH 1/3] fixes test case --- accounting.js | 25 ++++++++++++++++++++++--- tests/jasmine/core/formatNumberSpec.js | 1 + 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/accounting.js b/accounting.js index 755a3d5..9838cb0 100644 --- a/accounting.js +++ b/accounting.js @@ -215,10 +215,29 @@ */ 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); + 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 0 ? [integer,newMantissa].join('.') : integer; + + if(x > 4) { + var idx = newValue.length-1; + var lastNumber = Number(newValue[idx]) + 1; + newValue = newValue.substr(0,idx) + lastNumber; + } + + return newValue; }; diff --git a/tests/jasmine/core/formatNumberSpec.js b/tests/jasmine/core/formatNumberSpec.js index 6ee6a51..50c4a79 100644 --- a/tests/jasmine/core/formatNumberSpec.js +++ b/tests/jasmine/core/formatNumberSpec.js @@ -17,6 +17,7 @@ describe('formatNumber', 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' ); }); From 5e537e898a5351951242a65d431a8faf96ff4a48 Mon Sep 17 00:00:00 2001 From: Mike Walters Date: Thu, 28 Jul 2016 14:21:52 -0400 Subject: [PATCH 2/3] fixed --- accounting.js | 14 +++++++++----- tests/jasmine/core/formatNumberSpec.js | 5 ++++- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/accounting.js b/accounting.js index 9838cb0..0ee317b 100644 --- a/accounting.js +++ b/accounting.js @@ -229,18 +229,22 @@ var x = parseInt(mantissa[precision]); var newMantissa = mantissa.substr(0,precision); - var newValue = newMantissa.length > 0 ? [integer,newMantissa].join('.') : integer; + var fullNumber = Number([integer,newMantissa].join("")); if(x > 4) { - var idx = newValue.length-1; - var lastNumber = Number(newValue[idx]) + 1; - newValue = newValue.substr(0,idx) + lastNumber; + 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 * Alias: `accounting.format()` diff --git a/tests/jasmine/core/formatNumberSpec.js b/tests/jasmine/core/formatNumberSpec.js index 50c4a79..3a8449c 100644 --- a/tests/jasmine/core/formatNumberSpec.js +++ b/tests/jasmine/core/formatNumberSpec.js @@ -11,9 +11,12 @@ 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' ); From 620bfa6985c2125a8053434cf76959d5526ac7c0 Mon Sep 17 00:00:00 2001 From: Mike Walters Date: Thu, 13 Oct 2016 12:44:15 -0400 Subject: [PATCH 3/3] Fix for value being 0 (zero) ... should be packaged woith #152 pull request --- accounting.js | 4 ++++ tests/jasmine/core/formatMoneySpec.js | 1 + tests/jasmine/core/formatNumberSpec.js | 3 ++- 3 files changed, 7 insertions(+), 1 deletion(-) diff --git a/accounting.js b/accounting.js index 0ee317b..b902b72 100644 --- a/accounting.js +++ b/accounting.js @@ -217,6 +217,10 @@ precision = checkPrecision(precision, lib.settings.number.precision); var unformattedValue = lib.unformat(value).toString(); + if(unformattedValue == 0) { + return Number(0).toFixed(precision); + } + var number = unformattedValue.split("."); var integer = number[0]; 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 3a8449c..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' );