C# file size formatting library PrettySize 3.1 released

Pretty-print and calculate file sizes for C# and .NET with ease

PrettySizeHot on the heels of an update to our rust port of PrettySize we have a new release of PrettySize.NET that brings new features and capabilities to the best .NET library for formatting file sizes for human-readable output and display.

PrettySize 3.1, available on GitHub and via Nuget, has just been released and contains a number of improvements and requested features and newfound abilities to make handling file sizes (and not just formatting them) easier and more enjoyable.

Starting with a quick recap of PrettySize’s abilities for those of you that haven’t heard of it before:

using NeoSmart.PrettySize;

public void Main() {
    // Initializing directly with a raw byte count:
    var size = new PrettySize(bytes: 200);
    Console.WriteLine($"Size: {size}"); 
    // Prints "Size: 200 bytes"

    // Initializing via a unit-based helper function:
    var size1 = PrettySize.KiB(28);
    var size2 = PrettySize.Bytes(14336);
    var sum = size1 + size2;
    Console.WriteLine($"The total size is {sum}");
    // Prints "The total size is 42.00 KiB"
}

As you can see, PrettySize takes any arbitrary file size (in whatever unit you happen to have on hand) and automatically figures out how it should be displayed, figuring out

  • What the result of any mathematical operations on strongly-typed PrettySize values (wrapping file sizes) are
  • What unit to display the final size in (bytes, kilobytes, megabytes, etc)
  • What precision to use for the shown numeric size (just enough, but not too much)

New to version 3.x are the following:

  • The ability to perform math directly on PrettySize types, including adding or subtracting PrettySize values and multiplying or dividing a PrettySize value by a scalar numeric value.1
  • The ability to encapsulate/express negative sizes (such as the negative result of 4KiB - 8KiB),
  • The ability to format/print negative results, previously only available for unsigned values,
  • Comparison and equality operators for directly comparing PrettySize values rather than having to compare their underlying byte count (as exposed via the PrettySize.TotalBytes property).

There are some more improvements and enhancements under the hood, and a number of new helper functions modeled after the unit names to make it easier to instantiate PrettySize instances from non-byte file size values (e.g. directly from kilobytes). The interface should be more natural and ergonomic, and hopefully developers should find themselves reaching for PrettySize.TotalBytes far less than before.

If you would like to receive a notification the next time we release a nuget package for .NET or release resources for .NET Core and ASP.NET Core, you can subscribe below. Note that you'll only get notifications relevant to .NET programming and development by NeoSmart Technologies. If you want to receive email updates for all NeoSmart Technologies posts and releases, please sign up in the sidebar to the right instead.

Remember that PrettySize supports custom formatting of file sizes and has full support for both base-10 (kilobyte, megabyte, gigabyte) and base-2 (kibibyte, mebibyte, gibibyte — more often referred to as KiB, MiB, GiB, etc) units and you can choose between them, as well as determine how the unit names are styled and more:

// using NeoSmart.PrettySize

var size = PrettySize.Bytes(2048);

var formatted = size.Format(UnitBase.Base2, UnitStyle.Full);
Console.WriteLine(formatted); // Prints "2.00 Kebibytes"

var formatted2 = size.Format(UnitBase.Base10, UnitStyle.FullLower);
Console.WriteLine(formatted2); // Prints "2.05 kilobytes"

Minor Update
Version 3.1.1 is available on NuGet and includes a [Obsolete] backwards compatibility shim for a member that was misspelled in an earlier release.

The code is released as open source under the MIT license and the library is available free of charge for all uses, commercial or otherwise. Contributions to the GitHub repository are welcome.

Get PrettySize (free, cross-platform)

If you want to hear more from me about dotnet dev, follow me on twitter. To express your thanks or support, please star the repo on GitHub or retweet below:


  1. In the case of multiplication, the commutative inverse (multiplying a scalar numeric value by a strongly-typed PrettySize value) is also supported. For more about the caveats when implementing commutative mathematical operations on or between arbitrary types, read last week’s article on the rust release of PrettySize for some PLT-related matters. 

  • Similar Posts

    Craving more? Here are some posts a vector similarity search turns up as being relevant or similar from our catalog you might also enjoy.
    1. PrettySize: a .NET library for printing human-readable file sizes
    2. PrettySize for rust
    3. PrettySize 0.3 release and a weakness in rust's type system
    4. SecureStore: the open secrets container format
    5. Faster lookups, insertions, and in-order traversal than a red-black or AVL tree
  • Leave a Reply

    Your email address will not be published. Required fields are marked *