Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0a6aded

Browse files
committed
Fix josdejong#1512: bug in format notation engineering
1 parent d1451e2 commit 0a6aded

4 files changed

Lines changed: 50 additions & 7 deletions

File tree

HISTORY.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
# History
22

33

4+
# not yet published, version 5.10.1
5+
6+
- Fix #1512: format using notation `engineering` can give wrong results
7+
when the value has less significant digits than the number of digits in
8+
the output.
9+
10+
411
# 2019-05-08, version 5.10.0
512

613
- Fix `lib/header.js` not having filled in date and version. Thanks @kevjin.

src/utils/bignumber/formatter.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -174,11 +174,14 @@ exports.toEngineering = function (value, precision) {
174174
const newExp = e % 3 === 0 ? e : (e < 0 ? (e - 3) - (e % 3) : e - (e % 3))
175175

176176
// find difference in exponents, and calculate the value without exponent
177-
const expDiff = Math.abs(e - newExp)
178-
const valueWithoutExp = value.mul(Math.pow(10, expDiff - e))
177+
const valueWithoutExp = value.mul(Math.pow(10, -newExp))
179178

180-
return valueWithoutExp.toPrecision(precision).toString() +
181-
'e' + (e >= 0 ? '+' : '') + newExp.toString()
179+
let valueStr = valueWithoutExp.toPrecision(precision)
180+
if (valueStr.indexOf('e') !== -1) {
181+
valueStr = valueWithoutExp.toString()
182+
}
183+
184+
return valueStr + 'e' + (e >= 0 ? '+' : '') + newExp.toString()
182185
}
183186

184187
/**

src/utils/number.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -263,13 +263,17 @@ exports.toEngineering = function (value, precision) {
263263

264264
if (exports.isNumber(precision)) {
265265
// add zeroes to give correct sig figs
266-
if (precision > c.length) c = c.concat(zeros(precision - c.length))
266+
while (precision > c.length || (e - newExp) + 1 > c.length) {
267+
c.push(0)
268+
}
267269
} else {
268270
// concatenate coefficients with necessary zeros
269271
const significandsDiff = e >= 0 ? e : Math.abs(newExp)
270272

271273
// add zeros if necessary (for ex: 1e+8)
272-
if (c.length - 1 < significandsDiff) c = c.concat(zeros(significandsDiff - (c.length - 1)))
274+
while (c.length - 1 < significandsDiff) {
275+
c.push(0)
276+
}
273277
}
274278

275279
// find difference in exponents
@@ -278,7 +282,10 @@ exports.toEngineering = function (value, precision) {
278282
let decimalIdx = 1
279283

280284
// push decimal index over by expDiff times
281-
while (--expDiff >= 0) decimalIdx++
285+
while (expDiff > 0) {
286+
decimalIdx++
287+
expDiff--
288+
}
282289

283290
// if all coefficient values are zero after the decimal point and precision is unset, don't add a decimal value.
284291
// otherwise concat with the rest of the coefficients

test/function/string/format.test.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,19 @@ describe('format', function () {
119119
assert.strictEqual(math.format(0.000124, { notation: 'engineering', precision: 4 }), '124.0e-6')
120120
assert.strictEqual(math.format(0.000124, { notation: 'engineering', precision: 3 }), '124e-6')
121121
assert.strictEqual(math.format(1.24, { notation: 'engineering', precision: 3 }), '1.24e+0')
122+
123+
// less significant figures than precision, need to add zeros
124+
assert.strictEqual(math.format(10e3, { notation: 'engineering', precision: 2 }), '10e+3')
125+
assert.strictEqual(math.format(10e3, { notation: 'engineering', precision: 1 }), '10e+3')
126+
assert.strictEqual(math.format(100e3, { notation: 'engineering', precision: 2 }), '100e+3')
127+
assert.strictEqual(math.format(100e3, { notation: 'engineering', precision: 1 }), '100e+3')
128+
assert.strictEqual(math.format(100, { notation: 'engineering', precision: 2 }), '100e+0')
129+
assert.strictEqual(math.format(10, { notation: 'engineering', precision: 2 }), '10e+0')
130+
assert.strictEqual(math.format(10, { notation: 'engineering', precision: 1 }), '10e+0')
131+
assert.strictEqual(math.format(1, { notation: 'engineering', precision: 2 }), '1.0e+0')
132+
assert.strictEqual(math.format(10e-3, { notation: 'engineering', precision: 2 }), '10e-3')
133+
assert.strictEqual(math.format(100e-3, { notation: 'engineering', precision: 2 }), '100e-3')
134+
assert.strictEqual(math.format(100e-3, { notation: 'engineering', precision: 1 }), '100e-3')
122135
})
123136
})
124137

@@ -201,6 +214,19 @@ describe('format', function () {
201214
assert.strictEqual(math.format(bignumber(0.000124), { notation: 'engineering', precision: 4 }), '124.0e-6')
202215
assert.strictEqual(math.format(bignumber(0.000124), { notation: 'engineering', precision: 3 }), '124e-6')
203216
assert.strictEqual(math.format(bignumber(1.24), { notation: 'engineering', precision: 3 }), '1.24e+0')
217+
218+
// less significant figures than precision, need to add zeros
219+
assert.strictEqual(math.format(bignumber(10e3), { notation: 'engineering', precision: 2 }), '10e+3')
220+
assert.strictEqual(math.format(bignumber(10e3), { notation: 'engineering', precision: 1 }), '10e+3')
221+
assert.strictEqual(math.format(bignumber(100e3), { notation: 'engineering', precision: 2 }), '100e+3')
222+
assert.strictEqual(math.format(bignumber(100e3), { notation: 'engineering', precision: 1 }), '100e+3')
223+
assert.strictEqual(math.format(bignumber(100), { notation: 'engineering', precision: 2 }), '100e+0')
224+
assert.strictEqual(math.format(bignumber(10), { notation: 'engineering', precision: 2 }), '10e+0')
225+
assert.strictEqual(math.format(bignumber(10), { notation: 'engineering', precision: 1 }), '10e+0')
226+
assert.strictEqual(math.format(bignumber(1), { notation: 'engineering', precision: 2 }), '1.0e+0')
227+
assert.strictEqual(math.format(bignumber(10e-3), { notation: 'engineering', precision: 2 }), '10e-3')
228+
assert.strictEqual(math.format(bignumber(100e-3), { notation: 'engineering', precision: 2 }), '100e-3')
229+
assert.strictEqual(math.format(bignumber(100e-3), { notation: 'engineering', precision: 1 }), '100e-3')
204230
})
205231
})
206232
})

0 commit comments

Comments
 (0)