| src | ||
| tests | ||
| .gitignore | ||
| .gitlab-ci.yml | ||
| bytesized.nimble | ||
| LICENSE.md | ||
| README.md | ||
bytesized
a nim library for manipulating data storage units so you don't have to write boring code.
installation
nimble add bytesized
usage
Units enum
bytesized uses an enum called Units to represent different data storage units.
You can use Bytes, KiB, KB, MiB, MB, GiB, GB, TiB, TB, PiB, or PB
Just use Units.GiB or whatever unit you need
toBytes
toBytes converts a float value of another unit to bytes.
proc toBytes(input: float, inUnit: Units): int
example
const kibibytes:float = 1.5
let bytes:int = kilobytes.toBytes(Units.KiB)
bytes equals 1536
toUnit
toUnit is a umbrella term for all the procedures that convert one unit to another. It is very flexible and the easiest way to use bytesized.
It can be used with only an integer, for converting bytes to something else
proc toKiB(input: int): float
You can also specify the amount of decimal places to round to, otherwise, it defaults to 1 place
proc toMB(input:int, roundDigits:int): float
Another argument you can use is a Units enum value to tell the procedure which unit it's converting from.
However, this takes a float value for input since you won't be converting from bytes
proc toGiB(input:float, inUnit:Units): float
Or, you can use both at the same time
proc toTB(input:float, inUnit:Units, roundDigits:int): float
A full list of every toUnit procedure:
- toKiB
- toKB
- toMiB
- toMB
- toGiB
- toGB
- toTiB
- toTB
- toPiB
- toPB
They all work the same.
You can convert between kibibytes (1024) and kilobytes (1000), or any other units
example
const kibibytes:float = 1024.0
let kilobytes:float = kilobytes.toKB(Units.KiB)
kilobytes equals 1000.0
convertBytes
convertBytes is a procedure that is called by each toUnit procedure. You can use it but it's just an alias, and less verbose.
proc convertBytes(input:float, inUnit:Units, outUnit:Units, roundDigits:int): float
You can skip the roundDigits argument to default to 1 decimal place
proc convertBytes(input:float, inUnit:Units, outUnit:Units): float
If no inUnit argument is specified, it assumes the input is in bytes.
proc convertBytes(input:float, outUnit:Units, roundDigits:int): float
proc convertBytes(input:int, outUnit:Units, roundDigits:int): float
proc convertBytes(input:float, outUnit:Units): float
proc convertBytes(input:int, outUnit:Units): float
example
const kibibytes:float = 16000000.0
let gibibytes:float = convertBytes(kibibytes, Units.KB, Units.GiB, 3)
gibibytes equals 14.901