Function Repository Resource:

# QuantityString

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]:=
 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]=

### Scope (4)

Display the unit in the same way the FrontEnd does:

 In[4]:=
 Out[4]=
 In[5]:=
 Out[5]=

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

### Possible Issues (2)

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

Sjoerd Smit

## Version History

• 1.0.0 – 06 September 2022