View Source BetterNumber.SI (better_number v1.0.1)

Provides functions for formatting numbers using SI notation.

Summary

Functions

Link to this function

number_to_si(number, options \\ %{base: 1000, precision: 2, separator: "", trim: false, unit: ""})

View Source
@spec number_to_si(BetterNumber.t(), map() | Keyword.t()) :: String.t()

Format numbers using SI notation

Parameters

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

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

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

Examples

iex> BetterNumber.SI.number_to_si(1210000000, unit: "W")
"1.21GW"

iex> BetterNumber.SI.number_to_si(1210000000, unit: "W", precision: 1)
"1.2GW"

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

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

iex> BetterNumber.SI.number_to_si(1210000000)
"1.21G"

iex> BetterNumber.SI.number_to_si(Decimal.new(1210000000))
"1.21G"