Function Repository Resource:

MetricNumberString

Source Notebook

Convert raw numbers into metric system amounts for human readability

Contributed by: Michael Sollami

ResourceFunction["MetricNumberString"][x]

returns a human-readable version of the given number x.

Details and Options

ResourceFunction["MetricNumberString"] uses the standard metric system (SI) prefixes.
ResourceFunction["MetricNumberString"] always returns a formatted string.
ResourceFunction["MetricNumberString"] takes the following options:
"Decimals"Automaticthe number of decimal places after the decimal point
NumberPoint"."the decimal point character; e.g. in South Africa you might use a comma
"PrefixFunction"Identitycustom function which is given the computed prefix and formats a string
"PrefixShort"Trueuse short prefixes instead of long; e.g. "m" for "mega"
"Spacer"" "string that goes in between the number and unit

Examples

Basic Examples (1) 

Convert a number to a readable approximation with metric prefixes:

In[1]:=
ResourceFunction["MetricNumberString"] /@ {10^24, 1234567, 7!}
Out[1]=

Scope (1) 

MetricSize gives unique prefixes up to an octillion (1027):

In[2]:=
{ScientificForm[#], ResourceFunction["MetricNumberString"][#]} & /@ Table[10^i, {i, 1, 27, 3}] // Grid
Out[2]=

Options (4) 

Control the number of digits after the decimal point:

In[3]:=
ResourceFunction["MetricNumberString"][1234, "Decimals" -> 2]
Out[3]=

Use long prefixes:

In[4]:=
ResourceFunction["MetricNumberString"][1234, "PrefixShort" -> False]
Out[4]=

Change the spacing between the number and unit:

In[5]:=
ResourceFunction["MetricNumberString"][12^3, "Spacer" -> "", NumberPoint -> ","]
Out[5]=

Change the label formatting:

In[6]:=
ResourceFunction["MetricNumberString"][12^3, "PrefixFunction" -> (Capitalize[#] <> "-bytes" &)]
Out[6]=

Applications (1) 

Display memory in use in human-readable form:

In[7]:=
Text[ResourceFunction["MetricNumberString"][MemoryInUse[], "PrefixShort" -> False] <> "bytes"]
Out[7]=

Possible Issues (1) 

MetricSize will round in some cases, unless all decimals are asked for:

In[8]:=
{ResourceFunction["MetricNumberString"]@#, ResourceFunction["MetricNumberString"][#, "Decimals" -> All]} &[9999]
Out[8]=

Publisher

Michael Sollami

Version History

  • 2.0.0 – 25 March 2020
  • 1.0.0 – 13 January 2020

License Information