Function Repository Resource:

UnitAdjust

Source Notebook

Get a Quantity converted to the best prefix

Contributed by: Julián Laverde

ResourceFunction["UnitAdjust"][quan,{unit1,unit2,}]

return the Quantity quan converted to the unitj with the best prefix.

Details and Options

If the value of quan is given as a Quantity[number,unit] object, it is converted to unit1, unit2, etc., and the Quantity object with the best prefix is chosen.
ResourceFunction["UnitAdjust"] can take the option Method to determine the best prefix as follows:
"UnitCutoff"choose the prefix ≥ 1 that is closest to 1
"LogCutoff" valuechoose the prefix where Abs[Log[prefix]-value] is the smallest
Automaticequivalent to "LogCutoff" 3.5

Examples

Basic Examples (1) 

Optimize the units of a Quantity:

In[1]:=
ResourceFunction["UnitAdjust"][
 Quantity[10000, "Meters"], {"Centimeters", "Meters", "Kilometers"}]
Out[1]=

Scope (2) 

Choose among compatible units from different unit systems:

In[2]:=
ResourceFunction["UnitAdjust"][
 Quantity[0.0005, "Webers"/("Centimeters")^2], {"Teslas", "Gauss"}]
Out[2]=

For length units:

In[3]:=
ResourceFunction["UnitAdjust"][
 Quantity[0.02, "Feet"], {"Feet", "Miles", "Meters", "Centimeters"}]
Out[3]=

Options (3) 

UnitCutoff (1) 

Use "UnitCutoff" to select the prefix that is closest to 1:

In[4]:=
ResourceFunction["UnitAdjust"][
 Quantity[1000, "Grams"], {"Milligrams", "Grams", "Kilograms", "MetricTone"}, "Method" -> "UnitCutoff"]
Out[4]=

LogCutoff (1) 

Use "LogCutoff" to:

In[5]:=
ResourceFunction["UnitAdjust"][
 Quantity[900, "Grams"], {"Milligrams", "Grams", "Kilograms", "MetricTone"}, "Method" -> "LogCutoff" -> 2]
Out[5]=

Automatic (1) 

Automatic is equivalent to "LogCutoff" 3.5:

In[6]:=
ResourceFunction["UnitAdjust"][
 Quantity[900, "Grams"], {"Milligrams", "Grams", "Kilograms", "MetricTone"}, "Method" -> Automatic]
Out[6]=

Possible Issues (1) 

The given units must be compatible:

In[7]:=
CompatibleUnitQ["Teslas", "Meters"]
Out[7]=
In[8]:=
ResourceFunction["UnitAdjust"][
 Quantity[5, "Webers"/("Meters")^2], {"Teslas", "Meters"}]
Out[8]=

Neat Examples (1) 

Word cloud of temperature units:

In[9]:=
WordCloud[
 QuantityUnit@
  Table[ResourceFunction["UnitAdjust"][
    Quantity[i, "Kelvins"], {"Attokelvins", "Kelvins", "Decikelvins", "DegreesCelsius", "DegreesFahrenheit", "DegreesRankine", "DegreesReaumur", "WaterFreezingTemperature", "WaterTriplePointTemperature", "PlanckTemperature", "SolarEffectiveTemperature", "StoneyTemperature"}], {i, 0, 800, 0.1}], FontWeight -> Bold]
Out[9]=

Publisher

WolframSpecialProjects

Version History

  • 1.0.0 – 22 February 2021

License Information