17

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

rcollyer
  • 33,976
  • 7
  • 92
  • 191
TetstName123
  • 977
  • 7
  • 14

4 Answers4

24

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

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

Out[1]= 1
rcollyer
  • 33,976
  • 7
  • 92
  • 191
TetstName123
  • 977
  • 7
  • 14
  • 5
    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. – seismatica Aug 04 '14 at 18:18
6

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"]

Carl Woll
  • 130,679
  • 6
  • 243
  • 355
4

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 
Ku Zijie
  • 141
  • 3
  • First@Quantity[5,"Meters"] and Quantity[5,"Meters"] // First are the same thing. It works on Mathematica 9, 10, 11. – Ku Zijie Oct 20 '17 at 20:36
2

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

m_goldberg
  • 107,779
  • 16
  • 103
  • 257