PhoenixKit.Utils.Format (phoenix_kit v1.7.118)

Copy Markdown View Source

Workspace-shared formatting helpers for numeric / size values.

Summary

Functions

Formats a byte count as a human-readable size string.

Functions

bytes(value, opts \\ [])

@spec bytes(
  integer() | Decimal.t() | nil,
  keyword()
) :: String.t()

Formats a byte count as a human-readable size string.

Accepts an integer, Decimal, or nil. Returns the :unknown option string for nil (or any unrecognised input) and rounds to :decimals decimal places for the chosen unit. The unit auto-scales to B / KB / MB / GB based on the size.

Options

  • :decimals — decimal places for KB/MB/GB output. Default 1.
  • :unknown — string returned for nil or unrecognised inputs. Default "Unknown". Pass "0 B" to mimic the file-picker shape.
  • :base1024 (binary, default) or 1000 (decimal/SI). Some media-browser surfaces use the decimal convention to match what operating systems show; everything else defaults to the binary convention common in dev tooling.

Examples

iex> PhoenixKit.Utils.Format.bytes(0)
"0 B"

iex> PhoenixKit.Utils.Format.bytes(1500)
"1.5 KB"

iex> PhoenixKit.Utils.Format.bytes(1_500_000, decimals: 2)
"1.43 MB"

iex> PhoenixKit.Utils.Format.bytes(1_500_000, base: 1000, decimals: 2)
"1.5 MB"

iex> PhoenixKit.Utils.Format.bytes(nil, unknown: "0 B")
"0 B"