Description

A small library to format file sizes.

Package Information

Version0.3.1 (2015-Oct-29)
Homepage https://github.com/biozic/sizefmt
Repository https://github.com/biozic/sizefmt
LicenseBSL-1.0
CopyrightCopyright © 2014, Nicolas Sicard
AuthorsNicolas Sicard
Registered byNicolas Sicard
Dependencies none

Installation

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

dub.json
dub.sdl

Readme

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

Available versions

0.3.1 0.3.0 0.2.0 0.1.0 ~master