sizes 1.0.0

Basically core.time.Duration, but for byte sizes (file size)


To use this package, put the following dependency into your project's dependencies section:

dub.json
dub.sdl

sizes

A small library to format file sizes.

Example

import bytesize;
import std.format;

// toString defaults to SI units
assert(1.bytes.toString == "1 B");
assert(1.KiB.toString == "1.00 KiB");
assert(1.MiB.toString == "1.00 MiB");
assert(1.GiB.toString == "1.00 GiB");
assert(1.TiB.toString == "1.00 TiB");
assert(1.PiB.toString == "1.00 PiB");
assert(1.EiB.toString == "1.00 EiB");

// use %S format specifier to make them use decimal base
// %S will change it to %f internally
// all format specifiers are only used for manipulating the number
assert(1.bytes.format!"%S" == "1 B");
assert(1.KB.format!"%S" == "1.00 KB");
assert(1.MB.format!"%S" == "1.00 MB");
assert(1.GB.format!"%S" == "1.00 GB");
assert(1.TB.format!"%S" == "1.00 TB");
assert(1.PB.format!"%S" == "1.00 PB");
assert(1.EB.format!"%S" == "1.00 EB");

assert("%g".format(1024.bytes) == "1 KiB");
assert("%.2f".format(2_590_000.bytes) == "2.47 MiB");

Attribution

This library is basically a rewrite of sizefmt which focuses more on adding a new data type you can calculate with and simplicity than being a very flexible string serializer. Use sizefmt if you look for formatting sizes instead of calculating with them.

Authors:
Nicolas Sicard, webfreak
Dependencies:
none
Versions:
1.0.0 2018-Feb-05
~master 2018-Feb-05
Show all 2 versions
Download Stats:
  • 0 downloads today

  • 0 downloads this week

  • 1 downloads this month

  • 1 downloads total