Function Repository Resource:

QuantityString

Source Notebook

Convert a Quantity to a linear string in a customizable way

Contributed by: Sjoerd Smit

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.

Details

ResourceFunction["QuantityString"][quantity,"Canonical"] can also be used and does the same as ResourceFunction["QuantityString"][quantity].
The template will be filled with string versions of the TemplateBox arguments that are produced by ToBoxes[quantity].

Examples

Basic Examples (3) 

Define a Quantity composed of different units:

In[1]:=
q = \!\(\*
TagBox[
StyleBox[
RowBox[{"Quantity", "[", 
RowBox[{"1.24", ",", 
FractionBox[
RowBox[{
SuperscriptBox["\"\<Millimeters\>\"", "3"], "\"\<Kelvins\>\""}], 
RowBox[{"\"\<Kilograms\>\"", " ", 
SuperscriptBox["\"\<Seconds\>\"", "2"]}]]}], "]"}],
ShowSpecialCharacters->False,
ShowStringCharacters->True,
NumberMarks->True,
"NodeID" -> 6],
FullForm]\)
Out[1]=

Convert it to a simple String, using ^ and / to linearize the different powers:

In[2]:=
ResourceFunction["QuantityString"][q]
Out[2]=

Compare this with the InputForm of the QuantityUnit of this Quantity:

In[3]:=
QuantityUnit[q] // InputForm
Out[3]=

Scope (4) 

Display the unit in the same way the FrontEnd does:

In[4]:=
q = Quantity[1.24, (("Millimeters")^3 "Kelvins")/(
  "Kilograms" ("Seconds")^2)]
Out[4]=
In[5]:=
ResourceFunction["QuantityString"][q, "BoxForm"]
Out[5]=

Define your own output format:

In[6]:=
ResourceFunction["QuantityString"][q, "`1` (unit: `2`)"]
Out[6]=

See all the elements that you can access inside of the template:

In[7]:=
ResourceFunction["QuantityString"][q, TemplateExpression[{TemplateSlot[All]}]]
Out[7]=

Use this to define a more elaborate output:

In[8]:=
ResourceFunction[
 "QuantityString"][q, "`1` `2`, pronounced \"`3`\" or canonically: `4` (`5`)"]
Out[8]=

Not all types of quantities have only four elements:

In[9]:=
mixedQuantity = Quantity[MixedMagnitude[{5, 10}], MixedUnit[{"Feet", "Inches"}]]
Out[9]=
In[10]:=
ResourceFunction["QuantityString"][mixedQuantity, TemplateExpression[{TemplateSlot[All]}]]
Out[10]=

Most of the elements available in the template come from the box representation of the quantity:

In[11]:=
ToBoxes[mixedQuantity, StandardForm]
Out[11]=

The "BoxForm" output replaces the superscript primes with apostrophes:

In[12]:=
ResourceFunction["QuantityString"][mixedQuantity, "BoxForm"]
Out[12]=

The canonical form riffles the magnitudes with the canonical unit abbreviations:

In[13]:=
ResourceFunction["QuantityString"][mixedQuantity, "Canonical"]
Out[13]=

Convert different currencies. Note that the position of the currency sign is respected:

In[14]:=
AssociationMap[ResourceFunction["QuantityString"][#, "BoxForm"] &, Quantity[41.78, #] & /@ {"Yen", "Euros", "USDollars", "RussianRubles"}]
Out[14]=

Dated units are supported:

In[15]:=
q = Quantity[100., DatedUnit["Yen", 1990]]
Out[15]=
In[16]:=
ResourceFunction["QuantityString"][q, "BoxForm"]
Out[16]=

In the canonical form, the date will be typeset in the ISO format:

In[17]:=
ResourceFunction["QuantityString"][q, "Canonical"]
Out[17]=

Possible Issues (3) 

There is no guarantee that the strings can be converted back to quantities again:

In[18]:=
Quantity[MixedMagnitude[{5, 10}], MixedUnit[{"Feet"^2, "Inches"^2}]]
Out[18]=
In[19]:=
ResourceFunction["QuantityString"][%]
Out[19]=
In[20]:=
Interpreter["Quantity"]@%
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]:=
ResourceFunction["QuantityString"][
 Quantity[1, "USDollars"], "Canonical"]
Out[21]=

Use a template or the BoxForm appearance to typeset the canonical unit differently:

In[22]:=
ResourceFunction["QuantityString"][Quantity[1, "USDollars"], "`5``1`"]
Out[22]=
In[23]:=
ResourceFunction["QuantityString"][
 Quantity[1, "USDollars"], "BoxForm"]
Out[23]=

Use HoldForm to prevent evaluation of expressions in the first argument of Quantity:

In[24]:=
ResourceFunction["QuantityString"][
 Quantity[HoldForm[120/80], "MillimetersOfMercury"], "BoxForm"]
Out[24]=

Compare this to the evaluated form:

In[25]:=
ResourceFunction["QuantityString"][
 Quantity[120/80, "MillimetersOfMercury"], "BoxForm"]
Out[25]=

Alternatively you can use Row to typeset this correctly:

In[26]:=
ResourceFunction["QuantityString"][
 Quantity[Row[{120, 80}, "/"], "MillimetersOfMercury"], "BoxForm"]
Out[26]=

This will also work with the default formatting:

In[27]:=
ResourceFunction["QuantityString"][
 Quantity[Row[{120, 80}, "/"], "MillimetersOfMercury"]]
Out[27]=

Publisher

Sjoerd Smit

Requirements

Wolfram Language 13.0 (December 2021) or above

Version History

  • 1.0.1 – 12 June 2024
  • 1.0.0 – 06 September 2022

License Information