number v1.0.1 Number.SI

Provides functions for formatting numbers using SI notation.

Link to this section Summary


Format numbers using SI notation

Link to this section Functions

Link to this function

number_to_si(number, options \\ [])

number_to_si(number(), list()) :: String.t()

Format numbers using SI notation


  • number - A value to convert. Can be any value that implements Number.Conversion.to_float/1.

  • options - A keyword list of options. See the documentation below for all available options.


  • :base - Use 1024 if you wish to format bytes. Default: 1000

  • :separator - The string to place between the scaled number and the

               prefix + unit. Perhaps you want a space here. Default: ""
  • :unit - The unit of measurement, e.g. "M" for Meters. Default: ""

  • :precision - The number of decimal places to include. Default: 2

  • :trim - Trim trailing zeros. Default: false

Default configuration for these options can be specified in the Number application configuration.

config :number, si: [
                  separator: " ",
                  precision: 4,
                  trim: true


iex> Number.SI.number_to_si(1210000000, unit: "W")

iex> Number.SI.number_to_si(1210000000, unit: "W", precision: 1)

iex> Number.SI.number_to_si(1210000000, unit: "W", precision: 3, separator: " ")
"1.210 GW"

iex> Number.SI.number_to_si(1210000000, unit: "W", precision: 5, trim: true)

iex> Number.SI.number_to_si(1210000000)

iex> Number.SI.number_to_si(

iex> Number.SI.number_to_si('charlist')
** (ArgumentError) number must be a float, integer or implement [`Number.Conversion`](Number.Conversion.html) protocol, was 'charlist'