Wolfram Function Repository
Instant-use add-on functions for the Wolfram Language
Function Repository Resource:
Convert a Quantity to a linear string in a customizable way
ResourceFunction["QuantityString"][quantity] converts quantity to a linear string by abbreviating the canonical unit with standard short forms. | |
ResourceFunction["QuantityString"][quantity,"BoxForm"] converts quantity to a linear string that resembles the default typesetting in the front end. | |
ResourceFunction["QuantityString"][quantity, template] formats the output using a StringTemplate. |
Define a Quantity composed of different units:
In[1]:= | ![]() |
Out[1]= | ![]() |
Convert it to a simple String, using ^ and / to linearize the different powers:
In[2]:= | ![]() |
Out[2]= | ![]() |
Compare this with the InputForm of the QuantityUnit of this Quantity:
In[3]:= | ![]() |
Out[3]= | ![]() |
Display the unit in the same way the FrontEnd does:
In[4]:= | ![]() |
Out[4]= | ![]() |
In[5]:= | ![]() |
Out[5]= | ![]() |
Define your own output format:
In[6]:= | ![]() |
Out[6]= | ![]() |
See all the elements that you can access inside of the template:
In[7]:= | ![]() |
Out[7]= | ![]() |
Use this to define a more elaborate output:
In[8]:= | ![]() |
Out[8]= | ![]() |
Not all types of quantities have only four elements:
In[9]:= | ![]() |
Out[9]= | ![]() |
In[10]:= | ![]() |
Out[10]= | ![]() |
Most of the elements available in the template come from the box representation of the quantity:
In[11]:= | ![]() |
Out[11]= | ![]() |
The "BoxForm" output replaces the superscript primes with apostrophes:
In[12]:= | ![]() |
Out[12]= | ![]() |
The canonical form riffles the magnitudes with the canonical unit abbreviations:
In[13]:= | ![]() |
Out[13]= | ![]() |
Convert different currencies. Note that the position of the currency sign is respected:
In[14]:= | ![]() |
Out[14]= | ![]() |
Dated units are supported:
In[15]:= | ![]() |
Out[15]= | ![]() |
In[16]:= | ![]() |
Out[16]= | ![]() |
In the canonical form, the date will be typeset in the ISO format:
In[17]:= | ![]() |
Out[17]= | ![]() |
There is no guarantee that the strings can be converted back to quantities again:
In[18]:= | ![]() |
Out[18]= | ![]() |
In[19]:= | ![]() |
Out[19]= | ![]() |
In[20]:= | ![]() |
Out[20]= | ![]() |
The canonical unit string always places the unit after the magnitude, which is not always the typical way to typeset a unit:
In[21]:= | ![]() |
Out[21]= | ![]() |
Use a template or the BoxForm appearance to typeset the canonical unit differently:
In[22]:= | ![]() |
Out[22]= | ![]() |
In[23]:= | ![]() |
Out[23]= | ![]() |
Use HoldForm to prevent evaluation of expressions in the first argument of Quantity:
In[24]:= | ![]() |
Out[24]= | ![]() |
Compare this to the evaluated form:
In[25]:= | ![]() |
Out[25]= | ![]() |
Alternatively you can use Row to typeset this correctly:
In[26]:= | ![]() |
Out[26]= | ![]() |
This will also work with the default formatting:
In[27]:= | ![]() |
Out[27]= | ![]() |
Wolfram Language 13.0 (December 2021) or above
This work is licensed under a Creative Commons Attribution 4.0 International License