View Source CozySize.SI (cozy_size v0.1.0)
Provides utilities for sizes using SI-prefixed units.
Summary
Functions
Converts a number of bits to a {n, unit}
tuple.
Converts a number of bytes to a {n, unit}
tuple.
Converts a {n, unit}
tuple to a number of bits.
Converts a {n, unit}
tuple to a number of bytes.
Types
@type unit() ::
:b
| :kb
| :Mb
| :Gb
| :Tb
| :Pb
| :Eb
| :Zb
| :Yb
| :Rb
| :Qb
| :B
| :kB
| :MB
| :GB
| :TB
| :PB
| :EB
| :ZB
| :YB
| :RB
| :QB
Functions
@spec from_bits(CozySize.bits(), CozySize.from_opts()) :: {number(), unit()}
Converts a number of bits to a {n, unit}
tuple.
Examples
iex> CozySize.SI.from_bits(8, as: :bits) {8, :b}
iex> CozySize.SI.from_bits(8192, as: :bits) {8.19, :kb}
iex> CozySize.SI.from_bits(2 ** 120, as: :bits) {1329228, :Qb}
iex> CozySize.SI.from_bits(1024 10 * 11, as: :bits) {102.4, :Tb}
iex> CozySize.SI.from_bits(1024 10 * 11, as: :bits, precision: 4) {102.4, :Tb}
iex> CozySize.SI.from_bits(8, as: :bytes) {1, :B}
iex> CozySize.SI.from_bits(8192, as: :bytes) {1.02, :kB}
iex> CozySize.SI.from_bits(2 ** 120, as: :bytes) {166153.5, :QB}
iex> CozySize.SI.from_bits(1024 10 * 11, as: :bytes) {12.8, :TB}
iex> CozySize.SI.from_bits(1024 10 * 11, as: :bytes, precision: 4) {12.8, :TB}
@spec from_bytes(CozySize.bytes(), CozySize.from_opts()) :: {number(), unit()}
Converts a number of bytes to a {n, unit}
tuple.
Examples
iex> CozySize.SI.from_bytes(8, as: :bits) {64, :b}
iex> CozySize.SI.from_bytes(8192, as: :bits) {65.54, :kb}
iex> CozySize.SI.from_bytes(2 ** 120, as: :bits) {10633823.97, :Qb}
iex> CozySize.SI.from_bytes(1024 10 * 11, as: :bits) {819.2, :Tb}
iex> CozySize.SI.from_bytes(1024 10 * 11, as: :bits, precision: 4) {819.2, :Tb}
iex> CozySize.SI.from_bytes(8, as: :bytes) {8, :B}
iex> CozySize.SI.from_bytes(8192, as: :bytes) {8.19, :kB}
iex> CozySize.SI.from_bytes(2 ** 120, as: :bytes) {1329228, :QB}
iex> CozySize.SI.from_bytes(1024 10 * 11, as: :bytes) {102.4, :TB}
iex> CozySize.SI.from_bytes(1024 10 * 11, as: :bytes, precision: 4) {102.4, :TB}
@spec to_bits({number(), unit()}) :: CozySize.bits()
Converts a {n, unit}
tuple to a number of bits.
Examples
iex> CozySize.SI.to_bits({1, :b})
1
iex> CozySize.SI.to_bits({1, :kb})
1000
iex> CozySize.SI.to_bits({1.1, :kb})
1100
iex> CozySize.SI.to_bits({1, :B})
8
iex> CozySize.SI.to_bits({1, :kB})
8000
iex> CozySize.SI.to_bits({1.1, :kB})
8800
@spec to_bytes({number(), unit()}) :: CozySize.bits()
Converts a {n, unit}
tuple to a number of bytes.
Examples
iex> CozySize.SI.to_bytes({1, :b})
0.125
iex> CozySize.SI.to_bytes({1, :kb})
125
iex> CozySize.SI.to_bytes({1.1, :kb})
137.5
iex> CozySize.SI.to_bytes({1, :B})
1
iex> CozySize.SI.to_bytes({1, :kB})
1000
iex> CozySize.SI.to_bytes({1.1, :kB})
1100