sizefmt 0.3.1

A small library to format file sizes.


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

dub.json
dub.sdl

sizefmt

A small library to format file sizes.

Synopsis

unittest
{
    assert("%s".format(Size(0)) == "0 B");
    assert("%s".format(Size(1)) == "1 B");
    assert("%s".format(Size(42)) == "42 B");
    assert("%g".format(Size(1024)) == "1 KB");
    assert("%.2f".format(Size(2_590_000)) == "2.47 MB");
}

You can control the formatting by creating a new type with different options:

unittest
{
    enum Config config = {
        symbol: "O",
        unitName: "octet",
        unitNamePlural: "octets",
        prefixUse: PrefixUse.IEC,
        useNameIfNoPrefix: true
    };

	alias MySize = SizeBase!config;
    
    assert("%4.1f".format(MySize(0))         == "   0 octet");
    assert("%4.1f".format(MySize(1))         == "   1 octet");
    assert("%4.1f".format(MySize(42))        == "  42 octets");
    assert("%4.1f".format(MySize(1024))      == " 1.0 KiO");
    assert("%4.1f".format(MySize(2_590_000)) == " 2.5 MiO");
}

License: BSL 1.0

Copyright 2014-2015, Nicolas Sicard

Authors: Nicolas Sicard

Dependencies: none

Versions:
0.3.1 2015-Oct-29
0.3.0 2015-Apr-05
0.2.0 2014-Oct-31
0.1.0 2014-Oct-26
~master 2015-Oct-29
Show all 5 versions
Stats:
  • 0 downloads today

  • 0 downloads this week

  • 3 downloads this month

  • 489 downloads total