Function Repository Resource:

QuantityToSimpleString

Source Notebook

Format a Quantity as a human-readable string

Contributed by: Andrew Steinacher

ResourceFunction["QuantityToSimpleString"][quantity]

formats quantity as a human-readable string.

Details and Options

ResourceFunction["QuantityToSimpleString"] produces a String free of unnecessary whitespace characters, including newlines.
ResourceFunction["QuantityToSimpleString"] produces strings without special characters except for × (in scientific notation) and symbolic magnitudes such as π.
ResourceFunction["QuantityToSimpleString"] uses abbreviated forms of units when they are available.

Examples

Basic Examples (5) 

Format a Quantity as a simple String:

In[1]:=
ResourceFunction["QuantityToSimpleString"][Quantity[100, "Grams"]]
Out[1]=

Format a currency with proper padding:

In[2]:=
ResourceFunction["QuantityToSimpleString"][
 Quantity[123.2, "USDollars"]]
Out[2]=

Format a Quantity with an improper fraction:

In[3]:=
ResourceFunction["QuantityToSimpleString"][Quantity[5/2, "Cups"]]
Out[3]=

Format a Quantity with combined units:

In[4]:=
ResourceFunction["QuantityToSimpleString"][
 Quantity[0.34, "Grams"/"Grams"]]
Out[4]=

Use None to format a bare unit:

In[5]:=
ResourceFunction["QuantityToSimpleString"][
 Quantity[None, "LargeCalories"/"Grams"]]
Out[5]=

Scope (1) 

Format quantities in a wide variety of units and magnitudes:

In[6]:=
Grid[
 Table[
  Style[ResourceFunction["QuantityToSimpleString"]@
    Quantity[number, unit], FontSize -> 9],
  {number, {1, 2, 0.2, 1/2, 3/2, -3/2, -2.00*10^-6, N[Pi], 1.23, 1.2340`2.2, 0.00000234, 8675309, 8675309., 1230.00, Pi, None}},
  {unit, {"Grams", "LargeItems", "Tons", ("LargeCalories")/(
    "Grams"*"Meters"), HoldForm["Grams"/"Grams"], "USDollars"}}
  ],
 Frame -> All
 ]
Out[6]=

Applications (1) 

QuantityToSimpleString automatically handles singular and plural unit names:

In[7]:=
FullForm@StringRiffle[
  StringTemplate["`Amount` costs `Cost`", InsertionFunction -> ResourceFunction[
     "QuantityToSimpleString"]] /@ {
    <|"Item" -> "black beans", "Cost" -> Quantity[1.24, "USDollars"], "Amount" -> Quantity[1, "Cans"]|>,
    <|"Item" -> "pinto beans", "Cost" -> Quantity[2.1, "USDollars"], "Amount" -> Quantity[2, "Cans"]|>
    },
  "; "
  ]
Out[7]=

Properties and Relations (1) 

Compare QuantityToSimpleString (shown in black) to ToString (shown in red) and TextString (shown in blue):

In[8]:=
Grid[
 Flatten[Table[ {
    Style[
     ResourceFunction["QuantityToSimpleString"]@
      Quantity[number, unit], FontSize -> 9, FontWeight -> Bold],
    Style[ToString@Quantity[number, unit], FontSize -> 9, Darker@Green],
    Style[TextString@Quantity[number, unit], FontSize -> 9, Blue]
    }
   ,
   {number, {1, 2, 0.2, 1/2, 3/2, -3/2, -2.00*10^-6, N[Pi], 1.23, 1.2340`2.2, 0.00000234, 8675309, 8675309., 1230.00, Pi, None}},
   {unit, {"Grams", "LargeItems", "Tons", ("LargeCalories")/(
     "Grams"*"Meters"), HoldForm["Grams"/"Grams"], "USDollars"}}
   ], 1],
 Frame -> All, Alignment -> Left
 ]
Out[8]=

Possible Issues (2) 

Special characters appear for scientific notation:

In[9]:=
FullForm@ResourceFunction["QuantityToSimpleString"][
  Quantity[1.3*10^11, "Grams"]]
Out[9]=

Special characters also appear for symbolic magnitudes:

In[10]:=
FullForm@ResourceFunction["QuantityToSimpleString"][
  Quantity[Pi, "Grams"]]
Out[10]=

Publisher

Andrew Steinacher

Version History

  • 1.0.0 – 18 June 2020

Related Resources

Author Notes

Currency support has not been heavily tested against a wide range of currency units.

License Information