Is there a function which applied to a quantity returns its numerical value so I for instance can fill it into my Predictorfunction
?
4 Answers
Oops - found it! QuantityMagnitude[quantity]
does the job. For example,
In[1]:= QuantityMagnitude[Quantity[1, "Feet"]]
Out[1]= 1
-
5$\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$ Commented Aug 4, 2014 at 18:18
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"]
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
-
$\begingroup$
First@Quantity[5,"Meters"]
andQuantity[5,"Meters"] // First
are the same thing. It works on Mathematica 9, 10, 11. $\endgroup$– Ku ZijieCommented Oct 20, 2017 at 20:36
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