Skip to content

Commit

Permalink
Merge pull request #1163 from ericman314/develop
Browse files Browse the repository at this point in the history
Bug fix for engineering mode and precision
  • Loading branch information
josdejong committed Jul 14, 2018
2 parents 8e18a2b + 630fe11 commit c7e639c
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 8 deletions.
20 changes: 12 additions & 8 deletions src/utils/number.js
Expand Up @@ -245,8 +245,7 @@ exports.splitNumber = function (value) {
/**
* Format a number in engineering notation. Like '1.23e+6', '2.3e+0', '3.500e-3'
* @param {number | string} value
* @param {number} [precision=0] Optional number of decimals after the
* decimal point. Zero by default.
* @param {number} [precision] Optional number of significant figures to return.
*/
exports.toEngineering = function (value, precision) {
if (isNaN(value) || !isFinite(value)) {
Expand All @@ -261,11 +260,16 @@ exports.toEngineering = function (value, precision) {
// find nearest lower multiple of 3 for exponent
const newExp = e % 3 === 0 ? e : (e < 0 ? (e - 3) - (e % 3) : e - (e % 3))

// concatenate coefficients with necessary zeros
const significandsDiff = e >= 0 ? e : Math.abs(newExp)
if (exports.isNumber(precision)) {
// add zeroes to give correct sig figs
if (precision > c.length) c = c.concat(zeros(precision - c.length))
} else {
// concatenate coefficients with necessary zeros
const significandsDiff = e >= 0 ? e : Math.abs(newExp)

// add zeros if necessary (for ex: 1e+8)
if (c.length - 1 < significandsDiff) c = c.concat(zeros(significandsDiff - (c.length - 1)))
// add zeros if necessary (for ex: 1e+8)
if (c.length - 1 < significandsDiff) c = c.concat(zeros(significandsDiff - (c.length - 1)))
}

// find difference in exponents
let expDiff = Math.abs(e - newExp)
Expand All @@ -275,10 +279,10 @@ exports.toEngineering = function (value, precision) {
// push decimal index over by expDiff times
while (--expDiff >= 0) decimalIdx++

// if all coefficient values are zero after the decimal point, don't add a decimal value.
// if all coefficient values are zero after the decimal point and precision is unset, don't add a decimal value.
// otherwise concat with the rest of the coefficients
const decimals = c.slice(decimalIdx).join('')
const decimalVal = decimals.match(/[1-9]/) ? ('.' + decimals) : ''
const decimalVal = ((exports.isNumber(precision) && decimals.length) || decimals.match(/[1-9]/)) ? ('.' + decimals) : ''

const str = c.slice(0, decimalIdx).join('') +
decimalVal +
Expand Down
16 changes: 16 additions & 0 deletions test/function/string/format.test.js
Expand Up @@ -104,6 +104,22 @@ describe('format', function () {
it('should format positive floating point number to engineering notation', function () {
assert.equal(math.format(13308.0333333333, { precision: 11, notation: 'engineering' }), '13.308033333e+3')
})
it('should add or remove zeroes if necessary to output precision sig figs', function () {
assert.equal(math.format(12400, { notation: 'engineering', precision: 2 }), '12e+3')
assert.equal(math.format(12400, { notation: 'engineering', precision: 3 }), '12.4e+3')
assert.equal(math.format(124000, { notation: 'engineering', precision: 3 }), '124e+3')
assert.equal(math.format(124000, { notation: 'engineering', precision: 4 }), '124.0e+3')
assert.equal(math.format(12400, { notation: 'engineering', precision: 7 }), '12.40000e+3')
assert.equal(math.format(12400, { notation: 'engineering', precision: 8 }), '12.400000e+3')
assert.equal(math.format(12400, { notation: 'engineering', precision: 9 }), '12.4000000e+3')
assert.equal(math.format(-12400, { notation: 'engineering', precision: 7 }), '-12.40000e+3')
assert.equal(math.format(0.0124, { notation: 'engineering', precision: 7 }), '12.40000e-3')
assert.equal(math.format(0.00124, { notation: 'engineering', precision: 7 }), '1.240000e-3')
assert.equal(math.format(0.000124, { notation: 'engineering', precision: 7 }), '124.0000e-6')
assert.equal(math.format(0.000124, { notation: 'engineering', precision: 4 }), '124.0e-6')
assert.equal(math.format(0.000124, { notation: 'engineering', precision: 3 }), '124e-6')
assert.equal(math.format(1.24, { notation: 'engineering', precision: 3 }), '1.24e+0')
})
})

describe('bignumber', function () {
Expand Down

0 comments on commit c7e639c

Please sign in to comment.