Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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

share|improve this question
    
possible duplicate of Is there a way to display units in short label? –  eldo Sep 21 at 0:34
    
@eldo Haha, I asked that quation. No, its not the same. Displaying units in short label does not return a units in String. –  Yituo Sep 21 at 0:45

2 Answers 2

up vote 5 down vote accepted
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.

share|improve this answer
    
StringQ[%] gives False? –  Yituo Sep 21 at 0:16
    
@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 :) –  eldo Sep 21 at 0:27
    
@kguler I am very sorry. Thanks for your effort ! ++1 –  eldo Sep 21 at 1:54
    
@eldo, i too thought this was an almost-duplicate and would be really easy till i saw Yituo's edit:) –  kguler Sep 21 at 1:59
    
I'm not so sure this question is worth the effort that has gone into solving it. :) –  RunnyKine Sep 21 at 2:56

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"
share|improve this answer
1  
+1 however note that if e.g. m has a value this will fail. –  Mr.Wizard Sep 21 at 7:15
    
@Mr.Wizard. Thanks for pointing that out. I'll have to think about that. –  RunnyKine Sep 21 at 7:45
    
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 –  Yituo Sep 21 at 15:57

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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