josdejong/mathjs

Avoid unnecessary parentheses for Units with complex value when using `format`

オープン

#3,614 opened on 2025/12/17

 (4 件のコメント) (0 件のリアクション) (0 人の担当者)JavaScript (1,314 件のフォーク)batch import
category:unitsfeaturehelp wanted

Repository metrics

Stars
 (15,065 個のスター)
PR merge metrics
 (PR metrics pending)

説明

Describe the bug Some units are considered complex units, meaning they are treated differently to normal, "numeric" units. This for example is the case for the volt-ampere reactive, considered to be VAR by mathjs.

To Reproduce I am unsure why, but converting these units leads to unexpected results:

(new Unit(1, 'MW')).to('kW').format() // 1000 kW - correct
(new Unit(1, 'MVAR')).to('kVAR').format() // (1000) kVAR - weird
(new Unit(1, 'MVAR')).to('kW').format() // (1000i) kW - weird

I understand the want to represent these as complex numbers, but I think this is fairly out of place when dealing with units. It leads to unexpected conversions and format outputs.

Notice that using formatUnits avoids this, of course, since only the number part is affected by this:

(new Unit(1, 'MVAR')).to('kVAR').formatUnits() // kVAR
(new Unit(1, 'MVAR')).to('kW').formatUnits() // kW

コントリビューターガイド