From 2e1bbd14e5ae802b9ea4045ad228fb4d2dda2b05 Mon Sep 17 00:00:00 2001 From: john marinelli Date: Fri, 18 Dec 2015 18:57:27 -0800 Subject: [PATCH] initial commit --- lib/utils/NumberFormatter.js | 4 ++++ lib/utils/number.js | 18 ++++++++++++++++++ test/function/utils/format.test.js | 21 +++++++++++++++++++++ 3 files changed, 43 insertions(+) diff --git a/lib/utils/NumberFormatter.js b/lib/utils/NumberFormatter.js index 6f337ae16..95365d35b 100644 --- a/lib/utils/NumberFormatter.js +++ b/lib/utils/NumberFormatter.js @@ -41,6 +41,10 @@ function NumberFormatter (value) { this.exponent = exponent; } +NumberFormatter.prototype.toEngineering = function (precision) { + return '0'; +}; + /** * Format a number with fixed notation. * @param {number} [precision=0] Optional number of decimals after the diff --git a/lib/utils/number.js b/lib/utils/number.js index d57cdcd21..46845553d 100644 --- a/lib/utils/number.js +++ b/lib/utils/number.js @@ -60,6 +60,8 @@ exports.sign = function(x) { * For example '123.40' and '14000000' * 'exponential' Always use exponential notation. * For example '1.234e+2' and '1.4e+7' + * 'engineering' Always use engineering notation. + * For example '123.4e+0 and 14.0e+6' * 'auto' (default) Regular number notation for numbers * having an absolute value between * `lower` and `upper` bounds, and uses @@ -99,6 +101,7 @@ exports.sign = function(x) { * format(12.071, {notation: 'fixed'}); // '12' * format(2.3, {notation: 'fixed', precision: 2}); // '2.30' * format(52.8, {notation: 'exponential'}); // '5.28e+1' + * format(12345678, {notation: 'engineering'}); // '12.345678e+6' * * @param {number} value * @param {Object | Function | number} [options] @@ -148,6 +151,9 @@ exports.format = function(value, options) { case 'exponential': return exports.toExponential(value, precision); + case 'engineering': + return exports.toEngineering(value, precision); + case 'auto': return exports .toPrecision(value, precision, options && options.exponential) @@ -177,6 +183,18 @@ exports.toExponential = function(value, precision) { return new NumberFormatter(value).toExponential(precision); }; +/** + * Format a number in engineering notation. Like '1.23e+6', '2.3e+0', '3.500e-3' + * @param {number} value + * @param {number} [precision] Number of digits in formatted output. + * If not provided, the maximum available digits + * is used. + * @returns {string} str + */ +exports.toEngineering = function(value, precision) { + return new NumberFormatter(value).toEngineering(precision); +}; + /** * Format a number with fixed notation. * @param {number} value diff --git a/test/function/utils/format.test.js b/test/function/utils/format.test.js index df3101651..08c4317dd 100644 --- a/test/function/utils/format.test.js +++ b/test/function/utils/format.test.js @@ -60,6 +60,27 @@ describe('format', function() { }); + describe('engineering notation', function () { + it('should format positive single digit to engineering notation', function() { + assert.equal(math.format(3, { notation: 'engineering' }), '3.0e0'); + }); + xit('should format positive two digits to engineering notation', function() { + assert.equal(math.format(30, { notation: 'engineering' }), '30.0e0'); + }); + xit('should format positive three digits to engineering notation', function() { + assert.equal(math.format(300, { notation: 'engineering' }), '300.0e0'); + }); + xit('should format positive four digits to engineering notation', function() { + assert.equal(math.format(3000, { notation: 'engineering' }), '3.0e+3'); + }); + xit('should format positive uneven four digits to engineering notation', function() { + assert.equal(math.format(3001, { notation: 'engineering' }), '3.001e+3'); + }); + xit('should format positive uneven ten digits to engineering notation', function() { + assert.equal(math.format(3741293481, { notation: 'engineering' }), '3.741293481e+9'); + }); + }); + describe('bignumber', function () { var bigmath = math.create({precision: 20}); // ensure the precision is 20 digits