mirror of
https://github.com/josdejong/mathjs.git
synced 2026-01-25 15:07:57 +00:00
Fix #1813: bug in engineering notation for numbers of function format, sometimes resulting in needless trailing zeros
This commit is contained in:
parent
9f8564be4f
commit
3f6646f9e3
@ -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`,
|
||||
|
||||
@ -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
|
||||
|
||||
@ -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')
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user