diff --git a/HISTORY.md b/HISTORY.md index 509c12820..0d0b59162 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -3,6 +3,8 @@ # not yet published, version 6.6.3 +- 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`, `xor`, `to`, `in` as object keys. Thanks @Veeloxfire. - Fix `eigs` not using `config.epsilon`. diff --git a/src/type/unit/Unit.js b/src/type/unit/Unit.js index 76f9116e0..7bc8137c8 100644 --- a/src/type/unit/Unit.js +++ b/src/type/unit/Unit.js @@ -880,7 +880,7 @@ export const createUnitClass = /* #__PURE__ */ factory(name, dependencies, ({ other = this.clone() } - if (other._isDerived()) { + if (other._isDerived() || other.units.length === 0) { return other._denormalize(other.value) } else { return other._denormalize(other.value, other.units[0].prefix.value) diff --git a/test/unit-tests/type/unit/Unit.test.js b/test/unit-tests/type/unit/Unit.test.js index 037b7ad77..3808be575 100644 --- a/test/unit-tests/type/unit/Unit.test.js +++ b/test/unit-tests/type/unit/Unit.test.js @@ -231,6 +231,11 @@ describe('Unit', function () { const u = new Unit(math.fraction(5), 'cm') assert.strictEqual(u.toNumber('mm'), 50) }) + + it('should convert a unit with value only to a number', function () { + const u = Unit.parse('5', { allowNoUnits: true }) + assert.strictEqual(u.toNumber(), 5) + }) }) describe('toNumeric', function () { @@ -349,6 +354,11 @@ describe('Unit', function () { assert.strictEqual(u4.fixPrefix, true) }) + it('should convert a unitless quantity', function () { + const u = Unit.parse('5', { allowNoUnits: true }) + assert.strictEqual(u.toNumeric(), 5) + }) + it('should convert a binary prefixes (1)', function () { const u1 = new Unit(1, 'Kib') assert.strictEqual(u1.value, 1024)