diff --git a/HISTORY.md b/HISTORY.md index 0d0b59162..ff4e7a4ae 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -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`, diff --git a/src/utils/number.js b/src/utils/number.js index e65c03aaf..2402cb896 100644 --- a/src/utils/number.js +++ b/src/utils/number.js @@ -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 @@ -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 diff --git a/test/unit-tests/function/string/format.test.js b/test/unit-tests/function/string/format.test.js index 725d178c5..29cc2500d 100644 --- a/test/unit-tests/function/string/format.test.js +++ b/test/unit-tests/function/string/format.test.js @@ -71,6 +71,7 @@ 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') @@ -78,6 +79,10 @@ describe('format', function () { 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') }) @@ -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') @@ -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') @@ -194,6 +202,7 @@ 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') @@ -201,6 +210,10 @@ describe('format', function () { 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') }) @@ -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')