ByteCount (ByteCount v1.0.0)
Human-friendly byte sizes – construct, parse, format, and do math.
Examples
iex> ByteCount.gib(523) |> to_string()
"523.0 GiB"
iex> ByteCount.kib(42) |> ByteCount.format(style: :si, short: true)
"43.0k"
Summary
Functions
Add two ByteCount structs or byte count to an existing struct.
Divide two ByteCount structs or byte count to an existing struct.
Creates a %ByteCount{} from exabytes (1 EB = 10¹⁸ bytes).
Creates a %ByteCount{} from exbibytes (1 EiB = 2⁶⁰ bytes).
Return a human-readable string using the IEC style and default options.
Return a human-readable string representing of the byte count customizable using the following options
Creates a %ByteCount{} from gigabytes (1 GB = 10⁹ bytes).
Creates a %ByteCount{} from gibibytes (1 GiB = 2³⁰ bytes).
Creates a %ByteCount{} from kilobytes (1 KB = 10³ bytes).
Creates a %ByteCount{} from kibibytes (1 KiB = 2¹⁰ bytes).
Creates a %ByteCount{} from megabytes (1 MB = 10⁶ bytes).
Creates a %ByteCount{} from mebibytes (1 MiB = 2²⁰ bytes).
Multiply two ByteCount structs or byte count to an existing struct.
Construct a new struct from given byte count integer.
Parse byte count given as string.
Parse byte count or raise on error.
Creates a %ByteCount{} from petabytes (1 PB = 10¹⁵ bytes).
Creates a %ByteCount{} from pebibytes (1 PiB = 2⁵⁰ bytes).
Creates a %ByteCount{} from quettabytes (1 QB = 10³⁰ bytes).
Creates a %ByteCount{} from quebibytes (1 QiB = 2¹⁰⁰ bytes).
Creates a %ByteCount{} from ronnabytes (1 RB = 10²⁷ bytes).
Creates a %ByteCount{} from robibytes (1 RiB = 2⁹⁰ bytes).
Subtract two ByteCount structs or byte count to an existing struct.
Creates a %ByteCount{} from terabytes (1 TB = 10¹² bytes).
Creates a %ByteCount{} from tebibytes (1 TiB = 2⁴⁰ bytes).
Return byte count as an integer
Creates a %ByteCount{} from yottabytes (1 YB = 10²⁴ bytes).
Creates a %ByteCount{} from yobibytes (1 YiB = 2⁸⁰ bytes).
Creates a %ByteCount{} from zettabytes (1 ZB = 10²¹ bytes).
Creates a %ByteCount{} from zebibytes (1 ZiB = 2⁷⁰ bytes).
Types
@type format_opts() :: [ style: :iec | :si, short: boolean(), precision: non_neg_integer() ]
@type t() :: %ByteCount{bytes: non_neg_integer()}
Functions
Add two ByteCount structs or byte count to an existing struct.
Examples
iex> ByteCount.add(ByteCount.kb(1), ByteCount.b(23))
%ByteCount{bytes: 1023}
iex> ByteCount.add(ByteCount.kb(1), 1)
%ByteCount{bytes: 1001}
@spec b(non_neg_integer()) :: t()
Alias for new/1. Creates a %ByteCount{} from raw bytes.
Examples
iex> ByteCount.b(512)
%ByteCount{bytes: 512}
Divide two ByteCount structs or byte count to an existing struct.
Examples
iex> ByteCount.divide(ByteCount.kb(1), ByteCount.kb(1))
%ByteCount{bytes: 1}
iex> ByteCount.divide(ByteCount.kb(1), 1000)
%ByteCount{bytes: 1}
@spec eb(non_neg_integer()) :: t()
Creates a %ByteCount{} from exabytes (1 EB = 10¹⁸ bytes).
@spec eib(non_neg_integer()) :: t()
Creates a %ByteCount{} from exbibytes (1 EiB = 2⁶⁰ bytes).
Return a human-readable string using the IEC style and default options.
To customize formating see format/2.
@spec format(t(), format_opts()) :: String.t()
Return a human-readable string representing of the byte count customizable using the following options:
Options:
style: :iec | :si(default is :iec)short: true | false(default is false) →"43.0k"instead of"43.0 KiB"precision: 1 | 2 | ...(default is 1)
Examples
iex> ByteCount.kib(4) |> ByteCount.format()
"4.0 KiB"
iex> ByteCount.kb(4) |> ByteCount.format(style: :si, short: true, precision: 0)
"4k"
@spec gb(non_neg_integer()) :: t()
Creates a %ByteCount{} from gigabytes (1 GB = 10⁹ bytes).
@spec gib(non_neg_integer()) :: t()
Creates a %ByteCount{} from gibibytes (1 GiB = 2³⁰ bytes).
@spec kb(non_neg_integer()) :: t()
Creates a %ByteCount{} from kilobytes (1 KB = 10³ bytes).
@spec kib(non_neg_integer()) :: t()
Creates a %ByteCount{} from kibibytes (1 KiB = 2¹⁰ bytes).
@spec mb(non_neg_integer()) :: t()
Creates a %ByteCount{} from megabytes (1 MB = 10⁶ bytes).
@spec mib(non_neg_integer()) :: t()
Creates a %ByteCount{} from mebibytes (1 MiB = 2²⁰ bytes).
Multiply two ByteCount structs or byte count to an existing struct.
Examples
iex> ByteCount.multiply(ByteCount.kb(1), ByteCount.b(2))
%ByteCount{bytes: 2000}
iex> ByteCount.multiply(ByteCount.kb(1), 2)
%ByteCount{bytes: 2000}
@spec new(non_neg_integer()) :: t()
Construct a new struct from given byte count integer.
Examples
iex> ByteCount.new(1024)
%ByteCount{bytes: 1024}
Parse byte count given as string.
Examples
iex> ByteCount.parse("10")
{:ok, %ByteCount{bytes: 10}}
iex> ByteCount.parse("10 kb")
{:ok, %ByteCount{bytes: 10000}}
iex> ByteCount.parse("10k")
{:ok, %ByteCount{bytes: 10000}}
Parse byte count or raise on error.
Examples
iex> ByteCount.parse!("10")
%ByteCount{bytes: 10}
@spec pb(non_neg_integer()) :: t()
Creates a %ByteCount{} from petabytes (1 PB = 10¹⁵ bytes).
@spec pib(non_neg_integer()) :: t()
Creates a %ByteCount{} from pebibytes (1 PiB = 2⁵⁰ bytes).
@spec qb(non_neg_integer()) :: t()
Creates a %ByteCount{} from quettabytes (1 QB = 10³⁰ bytes).
@spec qib(non_neg_integer()) :: t()
Creates a %ByteCount{} from quebibytes (1 QiB = 2¹⁰⁰ bytes).
@spec rb(non_neg_integer()) :: t()
Creates a %ByteCount{} from ronnabytes (1 RB = 10²⁷ bytes).
@spec rib(non_neg_integer()) :: t()
Creates a %ByteCount{} from robibytes (1 RiB = 2⁹⁰ bytes).
Subtract two ByteCount structs or byte count to an existing struct.
Examples
iex> ByteCount.subtract(ByteCount.kb(3), ByteCount.kb(3))
%ByteCount{bytes: 0}
iex> ByteCount.subtract(ByteCount.kb(3), 3000)
%ByteCount{bytes: 0}
@spec tb(non_neg_integer()) :: t()
Creates a %ByteCount{} from terabytes (1 TB = 10¹² bytes).
@spec tib(non_neg_integer()) :: t()
Creates a %ByteCount{} from tebibytes (1 TiB = 2⁴⁰ bytes).
Return byte count as an integer
Examples
iex> ByteCount.kb(1) |> ByteCount.to_integer()
1000
@spec yb(non_neg_integer()) :: t()
Creates a %ByteCount{} from yottabytes (1 YB = 10²⁴ bytes).
@spec yib(non_neg_integer()) :: t()
Creates a %ByteCount{} from yobibytes (1 YiB = 2⁸⁰ bytes).
@spec zb(non_neg_integer()) :: t()
Creates a %ByteCount{} from zettabytes (1 ZB = 10²¹ bytes).
@spec zib(non_neg_integer()) :: t()
Creates a %ByteCount{} from zebibytes (1 ZiB = 2⁷⁰ bytes).