Skip to content

Commit

Permalink
Fix #1813: bug in engineering notation for numbers of function `forma…
Browse files Browse the repository at this point in the history
…t`, sometimes resulting in needless trailing zeros
  • Loading branch information
josdejong committed Apr 11, 2020
1 parent 9f8564b commit 3f6646f
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 6 deletions.
2 changes: 2 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

# not yet published, version 6.6.3

- Fix #1813: bug in engineering notation for numbers of function `format`,
sometimes resulting in needless trailing zeros.
- Fix #1808: methods `.toNumber()` and `.toNumeric()` not working on a
unitless unit.
- Fix #1645: not being able to use named operators `mod`, `and`, `not`, `or`,
Expand Down
11 changes: 5 additions & 6 deletions src/utils/number.js
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,8 @@ export function toEngineering (value, precision) {
return String(value)
}

const rounded = roundDigits(splitNumber(value), precision)
const split = splitNumber(value)
const rounded = roundDigits(split, precision)

const e = rounded.exponent
const c = rounded.coefficients
Expand All @@ -325,17 +326,15 @@ export function toEngineering (value, precision) {
}
} else {
// concatenate coefficients with necessary zeros
const significandsDiff = e >= 0 ? e : Math.abs(newExp)

// add zeros if necessary (for ex: 1e+8)
while (c.length - 1 < significandsDiff) {
// add zeros if necessary (for example: 1e+8 -> 100e+6)
const missingZeros = Math.abs(e - newExp) - (c.length - 1)
for (let i = 0; i < missingZeros; i++) {
c.push(0)
}
}

// find difference in exponents
let expDiff = Math.abs(e - newExp)

let decimalIdx = 1

// push decimal index over by expDiff times
Expand Down
16 changes: 16 additions & 0 deletions test/unit-tests/function/string/format.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,18 @@ describe('format', function () {
})
it('should format positive three digits to engineering notation', function () {
assert.strictEqual(math.format(300, { notation: 'engineering' }), '300e+0')
assert.strictEqual(math.format(320, { notation: 'engineering' }), '320e+0')
})
it('should format positive four digits to engineering notation', function () {
assert.strictEqual(math.format(3000, { notation: 'engineering' }), '3e+3')
})
it('should format positive uneven four digits to engineering notation', function () {
assert.strictEqual(math.format(3001, { notation: 'engineering' }), '3.001e+3')
})
it('should format a number without trailing zeros to engineering notation', function () {
assert.strictEqual(math.format(3010, { notation: 'engineering' }), '3.01e+3')
assert.strictEqual(math.format(452550000, { notation: 'engineering' }), '452.55e+6')
})
it('should format positive uneven ten digits to engineering notation', function () {
assert.strictEqual(math.format(3741293481, { notation: 'engineering' }), '3.741293481e+9')
})
Expand All @@ -89,6 +94,8 @@ describe('format', function () {
})
it('should format positive two digit floating point numbers to engineering notation', function () {
assert.strictEqual(math.format(0.01, { notation: 'engineering' }), '10e-3')
assert.strictEqual(math.format(0.011, { notation: 'engineering' }), '11e-3')
assert.strictEqual(math.format(0.0111, { notation: 'engineering' }), '11.1e-3')
})
it('should format positive three digit floating point numbers to engineering notation', function () {
assert.strictEqual(math.format(0.003, { notation: 'engineering' }), '3e-3')
Expand All @@ -101,6 +108,7 @@ describe('format', function () {
})
it('should format negative single digit floating point numbers to engineering notation', function () {
assert.strictEqual(math.format(-0.1, { notation: 'engineering' }), '-100e-3')
assert.strictEqual(math.format(-0.11, { notation: 'engineering' }), '-110e-3')
})
it('should format positive floating point number to engineering notation', function () {
assert.strictEqual(math.format(13308.0333333333, { precision: 11, notation: 'engineering' }), '13.308033333e+3')
Expand Down Expand Up @@ -194,13 +202,18 @@ describe('format', function () {
})
it('should format positive three digits to engineering notation', function () {
assert.strictEqual(math.format(bignumber(300), { notation: 'engineering' }), '300e+0')
assert.strictEqual(math.format(bignumber(320), { notation: 'engineering' }), '320e+0')
})
it('should format positive four digits to engineering notation', function () {
assert.strictEqual(math.format(bignumber(3000), { notation: 'engineering' }), '3e+3')
})
it('should format positive uneven four digits to engineering notation', function () {
assert.strictEqual(math.format(bignumber(3001), { notation: 'engineering' }), '3.001e+3')
})
it('should format a number without trailing zeros to engineering notation', function () {
assert.strictEqual(math.format(bignumber(3010), { notation: 'engineering' }), '3.01e+3')
assert.strictEqual(math.format(bignumber(452550000), { notation: 'engineering' }), '452.55e+6')
})
it('should format positive uneven ten digits to engineering notation', function () {
assert.strictEqual(math.format(bignumber(3741293481), { notation: 'engineering' }), '3.741293481e+9')
})
Expand All @@ -209,9 +222,12 @@ describe('format', function () {
})
it('should format positive single digit floating point numbers to engineering notation', function () {
assert.strictEqual(math.format(bignumber(0.1), { notation: 'engineering' }), '100e-3')
assert.strictEqual(math.format(bignumber(0.11), { notation: 'engineering' }), '110e-3')
})
it('should format positive two digit floating point numbers to engineering notation', function () {
assert.strictEqual(math.format(bignumber(0.01), { notation: 'engineering' }), '10e-3')
assert.strictEqual(math.format(bignumber(0.011), { notation: 'engineering' }), '11e-3')
assert.strictEqual(math.format(bignumber(0.0111), { notation: 'engineering' }), '11.1e-3')
})
it('should format positive three digit floating point numbers to engineering notation', function () {
assert.strictEqual(math.format(bignumber(0.003), { notation: 'engineering' }), '3e-3')
Expand Down

0 comments on commit 3f6646f

Please sign in to comment.