Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Floating point errors #152

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
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
35 changes: 31 additions & 4 deletions accounting.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<diff; i++)
mantissa += "0";
}

var x = parseInt(mantissa[precision]);
var newMantissa = mantissa.substr(0,precision);
var fullNumber = Number([integer,newMantissa].join(""));

if(x > 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
Expand Down
1 change: 1 addition & 0 deletions tests/jasmine/core/formatMoneySpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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' );
Expand Down
9 changes: 7 additions & 2 deletions tests/jasmine/core/formatNumberSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,25 @@ 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' );
expect( accounting.formatNumber(123.456789, 3) ).toBe( '123.457' );
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' );

});

Expand Down