Is there a function which applied to a quantity returns its numerical value so I for instance can fill it into my Predictorfunction?
- 33,976
- 7
- 92
- 191
- 977
- 7
- 14
-
you maybe want to accept an answer to this – george2079 Oct 20 '17 at 16:17
4 Answers
Oops - found it! QuantityMagnitude[quantity] does the job. For example,
In[1]:= QuantityMagnitude[Quantity[1, "Feet"]]
Out[1]= 1
- 33,976
- 7
- 92
- 191
- 977
- 7
- 14
-
5If 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@*Quantityin version 10) or/. Quantity[x_, y_] :> xto the end of your expression. Examples. – seismatica Aug 04 '14 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"]
- 130,679
- 6
- 243
- 355
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
- 141
- 3
-
First@Quantity[5,"Meters"]andQuantity[5,"Meters"] // Firstare the same thing. It works on Mathematica 9, 10, 11. – Ku Zijie Oct 20 '17 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
- 107,779
- 16
- 103
- 257
- 61
- 4