14
$\begingroup$

Is there a function which applied to a quantity returns its numerical value so I for instance can fill it into my Predictorfunction?

$\endgroup$
  • $\begingroup$ you maybe want to accept an answer to this $\endgroup$ – george2079 Oct 20 '17 at 16:17
20
$\begingroup$

Oops - found it! QuantityMagnitude[quantity] does the job. For example,

In[1]:= QuantityMagnitude[Quantity[1, "Feet"]]

Out[1]= 1
$\endgroup$
  • 4
    $\begingroup$ If you want to remove the units in an expression regardless of what level that unit resides on, you can tack /. Quantity :> Composition[QuantityMagnitude, Quantity] (/. Quantity :> QuantityMagnitude@*Quantity in version 10) or /. Quantity[x_, y_] :> x to the end of your expression. Examples. $\endgroup$ – seismatica Aug 4 '14 at 18:18
3
$\begingroup$

To temporary disable Quantity for all of your functions,

SetSystemOptions["DataOptions" -> "ReturnQuantities" -> False];

In[5]:= DateDifference[DateList[{2017, 11, 11}], DateList[]]
Out[5]= Quantity[-21.5912, "Days"]
In[6]:= oldSystemOptions = SystemOptions["DataOptions"]; 
        SetSystemOptions["DataOptions" -> "ReturnQuantities" -> False];
In[7]:= DateDifference[DateList[{2017, 11, 11}], DateList[]]
Out[7]= -21.5908

This is a neat trick to use. This works on Mathematica 10 & 11. Or simply use First.

In[12]:= First@DateDifference[DateList[{2017, 11, 11}], DateList[]]
Out[12]= -21.5869 
$\endgroup$
  • $\begingroup$ First@Quantity[5,"Meters"] and Quantity[5,"Meters"] // First are the same thing. It works on Mathematica 9, 10, 11. $\endgroup$ – Ku Zijie Oct 20 '17 at 20:36
3
$\begingroup$

As already mentioned, the function you're looking for is QuantityMagnitude. However, I think single argument QuantityMagnitude is error prone. It is much better to give it a second argument:

QuantityMagnitude[Quantity[1, "Feet"]]
QuantityMagnitude[Quantity[1, "Feet"], "Meters"]
QuantityMagnitude[Quantity[1, "Feet"], "Seconds"]

1

381/1250

Quantity::compat: Feet and Seconds are incompatible units

QuantityMagnitude[Quantity[1, "Feet"], "Seconds"]

$\endgroup$
2
$\begingroup$

First does work — try use // First after the expression to apply the First function such as below. But QuantityMagnitude is more proper so you can use either. First is for arrays and lists.

Quantity[5,"Meters"] // First

5

Quantity[5,"Meters"] // QuantityMagnitude

5

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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