diff --git a/index.js b/index.js index 8bc2286..ee965d3 100644 --- a/index.js +++ b/index.js @@ -68,6 +68,17 @@ function formatter(options) { overrideOptions = overrideOptions || {}; if (number || number === 0) { + if (typeof number === 'number') { + //get rid of the possible "e-" + var parts = number.toString().split('e-'); + if (parts.length === 2) { + var digits = (parts[0].replace('.', '').replace('-', '')).length - 1 + Number(parts[1]); + //maximum number of digits is 20 + //see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toFixed + digits = digits > 20 ? 20 : digits; + number = number.toFixed(digits); + } + } number = '' + number;//convert number to string if it isn't already } else { return ''; diff --git a/test/index.js b/test/index.js index 59c7974..683940e 100644 --- a/test/index.js +++ b/test/index.js @@ -502,3 +502,16 @@ describe('£68,932/items with no separators', function () { expect(formatFactory({prefix: '£', suffix: '/item'})(68932, {noSeparator: true})).to.be('£68932/item'); }); }); + +//scientific notation +describe('scientific notation', function() { + it('get rid of "e-"', function () { + expect(formatFactory()(1.23456e-10)).to.be('0.000000000123456'); + }); + it('round', function () { + expect(formatFactory({round: 2})(1.4551915228366852e-11)).to.be('0.00'); + }); + it('truncate', function () { + expect(formatFactory({truncate: 2})(1.29995068248398e-16)).to.be('0.00'); + }); +});