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.

Selecting a Free-Form input cell with the mouse I can query something like:

convert 1 atm to pascals

and get as a result

101\[ThinSpace]325\[ThickSpace]Pa (pascals)

I can take that number and manually enter it into a variable (I can't cut and paste it since the space turns the cut and pasted value into (101)(325)).

Is there a way to just extract the numeric portion of the result and assign it to a variable?

share|improve this question
add comment

2 Answers

up vote 6 down vote accepted

You can use a slightly different query: (convert 1 atm to pascals)/pascals

enter image description here

The number in this case is a regular number (not a string or some other exotic construction.)


If you instead use Wolfram|Alpha query (shortcut ==), you get a lot of results:

enter image description here

with a + icon on the right of each. Clicking on the + gives a menu where you can choose a format:

enter image description here

In particular the 'Number data' format will then paste something like

WolframAlpha["convert 1 atm to pascals", {{"Result", 1}, "NumberData"}]
101325

into the notebook. This doesn't help you much the first time, but can be used programmatically for subsequent calculations.

share|improve this answer
    
WolframAlpha["(convert 1 atm to pascals)/pascals", {{"Result", 1}, "NumberData"}] works very nicely. Thanks! –  Peeter Joot Mar 4 '12 at 5:30
add comment

I am not sure there is a simple solution to this.

enter image description here
enter image description here

result = %;
result //FullForm

WolframAlphaResult["convert 1 atm to pascals", List[List["Result", 1], "Content"]]

Copy As... and various other attempts to get at the result don't work.

Interestingly, copying and pasting the result into somewhere other than Mathematica (e.g. an answer window here) gives the following, which suggests a way around the issue.

\!\(\*
FrameBox[    StyleBox[    FormBox[    StyleBox[    RowBox[{
RowBox[{"101", "\[ThinSpace]", "325"}], " ", " ", "\[ThickSpace]",   " ", 
StyleBox["\<\"Pa\"\>",  FontFamily->"Helvetica",  FontSize->Smaller], " ", 
StyleBox[
RowBox[{"\<\"(\"\>", "", "\<\"pascals\"\>", "", "\<\")\"\>"}],
StripOnInput->False, LineIndent->0,
LinebreakAdjustments->{1, 100, 1, 0, 100},
LineColor->GrayLevel[0.6], FrontFaceColor->GrayLevel[0.6], BackFaceColor->GrayLevel[0.6],
GraphicsColor->GrayLevel[0.6],   FontFamily->"Helvetica",
FontSize->Smaller,  FontColor->GrayLevel[0.6]]}], ZeroWidthTimes->False,  LineIndent->0,
LinebreakAdjustments->{1, 100, 1, 0, 100}],
TraditionalForm], "Output",
ScriptLevel->0, FontFamily->"Times", FontSize->14, Background->None],
BaseStyle->{Plain}, FrameMargins->10, FrameStyle->GrayLevel[0.85], RoundingRadius->5,
StripOnInput->False]\)

The part specification might be specific to the case in question, but something like the following seems to work.

 ToExpression@
 StringReplace[ToBoxes[result][[1, 1, 1, 1, 1, 1, 1, 1, 1]], 
  x__ ~~ "\[ThinSpace]" ~~ y__ :> x ~~ y]
share|improve this answer
add comment

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.