14
$\begingroup$

I observe the following in Wolfram Language:

Quantity[ 1, "Kilowatt hours" ] (* or "Kilowatts" * "Hours" *)

(* 1 h kW *)

That looks rather unconventional and in Wolfram Alpha it is done as kW h, for example: kW vs. kWh

How can this be achieved in Mathematica being compatible to the units framework?

EDIT:

Some more peculiarities.

Quantity[ 1, "Megawatt hours" ]
(* 1 h MW *)

(* but *)

Quantity[ 1, "USDollars" / "Megawatt hours" ]
(* Quantity: Unable to interpret unit specification *)

It is not the age of energy as of yet. ;-)

$\endgroup$
5
  • 1
    $\begingroup$ it seems to be baked into the display by frontend. Compare Quantity[1, "Hours"*"Kilowatts"] and Quantity[1, "Kilowatts"*"Hours"] they both give 1 h kW May be it uses some internal ordering based on letters or something like that? $\endgroup$
    – Nasser
    Commented Mar 21, 2022 at 12:20
  • 2
    $\begingroup$ I do not think there is a way to change that. One can try to use $PrePrint to change the display, but it is tricky for this kind of output. Better learn to live with it :) You could submit request to WRI to change this and see what they say? $\endgroup$
    – Nasser
    Commented Mar 21, 2022 at 12:23
  • 1
    $\begingroup$ Quantity[1, "USDollars"/("Kilowatts"*"Hours")] seems to solve your dollar issue. FYI, it seems to be sorting alphabetically by the unit name. $\endgroup$
    – Edmund
    Commented Mar 21, 2022 at 12:35
  • 8
    $\begingroup$ @gwr It is a new kind of units :) $\endgroup$
    – Nasser
    Commented Mar 21, 2022 at 12:46
  • 2
    $\begingroup$ If you are really desperate you could do Unprotect[RowBox]; RowBox[{"\"h\"", "\[ThinSpace]", "\"kW\""}] := RowBox[{"kWh"}]. But that is maybe not the right way to eat cookies. $\endgroup$ Commented Mar 21, 2022 at 12:59

1 Answer 1

14
$\begingroup$
QuantityUnits`Private`getCompoundUnitBoxes@Quantity[_, "Hours" "Kilowatts"] := 
 RowBox[{"\"kW\"", "\[ThinSpace]", "\"h\""}]

QuantityUnits`Private`getCompoundUnitBoxes@Quantity[_, _/("Hours" "Kilowatts")] :=
 RowBox[{"\"per\"", "\"kW\"", "\[ThinSpace]", "\"h\""}]

enter image description here


Update for Version 14 or Earlier

At least since version 14, the design of Quantity seems to be changed. Now we need the following code instead:

QuantityUnits`Private`getCompoundUnitBoxes["Hours" "Kilowatts"] := 
 RowBox[{"\"kW\"", "\[ThinSpace]", "\"h\""}]

QuantityUnits`Private`ToNDBoxes[{}, {"Hours", "Kilowatts"}] := 
 RowBox[{"\"per\"", "\"kW\"", "\[ThinSpace]", "\"h\""}]
$\endgroup$
2
  • $\begingroup$ It seems that this is not working anymore as of v14? $\endgroup$
    – gwr
    Commented Mar 11 at 14:58
  • 1
    $\begingroup$ @gwr Luckily it's not too hard to fix. See my update :) . $\endgroup$
    – xzczd
    Commented Mar 11 at 16:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.