Number formatting that produces strings suitable for the digits widget.
Examples
iex> Drafter.Format.compact(12_000)
"12k"
iex> Drafter.Format.bytes(1_048_576)
"1MB"
iex> Drafter.Format.percent(0.42, as_ratio: true)
"42%"
Summary
Functions
Formats a byte count with a binary unit: B, KB, MB, GB, or TB.
Abbreviates a number with a magnitude suffix: k, M, B, or T.
Formats a number as a percentage.
Functions
Formats a byte count with a binary unit: B, KB, MB, GB, or TB.
Units are powers of 1024.
Examples
iex> Drafter.Format.bytes(512)
"512B"
iex> Drafter.Format.bytes(1_536)
"1.5KB"
iex> Drafter.Format.bytes(1_048_576)
"1MB"
Abbreviates a number with a magnitude suffix: k, M, B, or T.
Values below 1000 carry no suffix. A fractional result keeps one decimal place.
Examples
iex> Drafter.Format.compact(999)
"999"
iex> Drafter.Format.compact(12_000)
"12k"
iex> Drafter.Format.compact(1_500_000)
"1.5M"
iex> Drafter.Format.compact(-2_400)
"-2.4k"
Formats a number as a percentage.
Options
:decimals- decimal places to keep (default0):as_ratio- treat the value as a0..1ratio and multiply it by 100 (defaultfalse)
Examples
iex> Drafter.Format.percent(7)
"7%"
iex> Drafter.Format.percent(99.5, decimals: 1)
"99.5%"
iex> Drafter.Format.percent(0.42, as_ratio: true)
"42%"