9
$\begingroup$

Is there a way to convert compound units to string? For example, I have "Newtons"/"Meters"^2, how to I convert it to "N/m^2"? I know If I do the following

QuantityForm[QuantityUnit[Quantity[1, "Newtons"/ "Meters"^2]], "Abbreviation"]

It will give me "N"/("m")^2, but not "N/m^2".

p.s. The reason I want to do this is that I am writing a function that will copy quantities to excel. After copying units are not displaying correctly

$\endgroup$
2
  • $\begingroup$ possible duplicate of Is there a way to display units in short label? $\endgroup$
    – eldo
    Sep 21, 2014 at 0:34
  • $\begingroup$ @eldo Haha, I asked that quation. No, its not the same. Displaying units in short label does not return a units in String. $\endgroup$
    – Yituo
    Sep 21, 2014 at 0:45

3 Answers 3

5
$\begingroup$
stringUnits =  Module[{qf = QuantityForm[QuantityUnit[#], "Abbreviation"], boxes},
  boxes = Cases[ToBoxes[qf], StyleBox[RowBox[{x___}], sty_] :> x];
  StringJoin[ boxes /. x_String :> ToExpression[x] /. Null -> " "  /. 
       SuperscriptBox[a_, b_] :> StringJoin[ToString@a, "^", ToString@b]]] &;

stringUnits @ Quantity["Newton/Meters^2"]
(* "N / m^2" *)
Head[%]
(* String *)

stringUnits @ Quantity[1.84, ("Meters*Pascals/Farads^2")]
(* "m Pa / F^2" *)
Head[%]
(* String *)

Works in Version 10 and 9.

$\endgroup$
7
  • $\begingroup$ StringQ[%] gives False? $\endgroup$
    – Yituo
    Sep 21, 2014 at 0:16
  • $\begingroup$ @kguler (1) This question is a duplicate of mathematica.stackexchange.com/questions/60248/… which you answered one hour ago and (2) it doesn't export to Excel. (3) Let me, therefore, post my own deficient answer :) $\endgroup$
    – eldo
    Sep 21, 2014 at 0:27
  • $\begingroup$ @kguler I am very sorry. Thanks for your effort ! ++1 $\endgroup$
    – eldo
    Sep 21, 2014 at 1:54
  • $\begingroup$ @eldo, i too thought this was an almost-duplicate and would be really easy till i saw Yituo's edit:) $\endgroup$
    – kglr
    Sep 21, 2014 at 1:59
  • $\begingroup$ I'm not so sure this question is worth the effort that has gone into solving it. :) $\endgroup$
    – RunnyKine
    Sep 21, 2014 at 2:56
11
$\begingroup$

The following works for V10. First we define some abbreviation rules:

rule = {"Newtons" :> N, "Meters" :> m, "Pascals" :> Pa, "Farads" :> F}; (* add more rules here *)

Then:

unit = TextString[QuantityUnit[Quantity[1, "Newtons/Meters^2"]] /. rule]
"N/m^2"
StringQ[unit]

True

TextString[QuantityUnit[Quantity[1, "Meters*Pascals/Farads^2"]] /. rule]
"(m*Pa)/F^2"
$\endgroup$
3
  • 1
    $\begingroup$ +1 however note that if e.g. m has a value this will fail. $\endgroup$
    – Mr.Wizard
    Sep 21, 2014 at 7:15
  • $\begingroup$ @Mr.Wizard. Thanks for pointing that out. I'll have to think about that. $\endgroup$
    – RunnyKine
    Sep 21, 2014 at 7:45
  • $\begingroup$ Thanks, this will work for specific cases.I guess it will work for my purpose for now, but may need to give it more thougths when a general solution is needed $\endgroup$
    – Yituo
    Sep 21, 2014 at 15:57
4
$\begingroup$

I recently published a ResourceFunction for this:

ResourceFunction["QuantityString"][Quantity[1, "Newtons"/ "Meters"^2]]

1 N/m^2

$\endgroup$

Your Answer

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

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