NeoSmart.PrettySize 3.1.0 The ID prefix of this package has been reserved for one of the owners of this package by NuGet.org. Prefix Reserved

.NET Standard 1.3 .NET Framework 2.0
There is a newer version of this package available.
See the version list below for details.
Install-Package NeoSmart.PrettySize -Version 3.1.0
dotnet add package NeoSmart.PrettySize --version 3.1.0
<PackageReference Include="NeoSmart.PrettySize" Version="3.1.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add NeoSmart.PrettySize --version 3.1.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: NeoSmart.PrettySize, 3.1.0"
#r directive can be used in F# Interactive, C# scripting and .NET Interactive. Copy this into the interactive tool or source code of the script to reference the package.
// Install NeoSmart.PrettySize as a Cake Addin
#addin nuget:?package=NeoSmart.PrettySize&version=3.1.0

// Install NeoSmart.PrettySize as a Cake Tool
#tool nuget:?package=NeoSmart.PrettySize&version=3.1.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

PrettySize, a .NET library for dealing with and formatting file sizes

PrettySize is a .NET (2.0+ with full .NET Core/.NET 6 support) library for dealing with and formatting/pretty-printing sizes of all kind. It was originally designed to make automatically formatting a file size (in terms of converting to the correct unit and choosing just the right amount of decimal precision) for pretty printing for human consumption a breeze, and has since evolved to include additionally functionality.

Installation

PrettySize is published as a NuGet package, meaning installation is as simple as looking up "PrettySize" in the Visual Studio package manager or by typing the following in the Visual Studio "Package Manager Console":

Install-Package NeoSmart.PrettySize

Use

The PrettySize package exposes the entirety of its surface area via the PrettySize struct/class under the NeoSmart.PrettySize namespace. PrettySize is a readonly struct wrapper around a file size and exposes strongly-typed size-related operations via that interface.

Creating a PrettySize instance can be done directly (given a raw file size in terms of bytes) or via any of the static helper functions/constructors that provide for unit-based construction. Both base-10 (KB, MB, etc) and base-2 (KiB, MiB, etc) units are supported via the same interface:

using NeoSmart.PrettySize;

public void Main() {
    // Initializing directly via the default constructor:
    var size = new PrettySize(200); // 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"
}

The benefits of using a strongly-typed object instead of passing around long or ulong file sizes include (as shown above) the ability to add sizes with different units, being prevented from mixing up types and performing mathematical operations that make no sense (you can safely add/subtract two PrettySize values or you can multiply/divide a PrettySize value and a scalar long value, but you can't do something like accidentally multiply two file sizes together), and having something that you can always just print or convert to a string without worrying about formatting and readability.

For more control over how sizes are formatted as text, you can use PrettySize.Format() method, which lets you specify the units to be used (base-2 or base-10) and how unit names are spelled out (abbreviated, unabridged, lowercase, etc):

// 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"

Use with scalar size values

The PrettySize project also includes some constants and static methods that can be used to create and format scalar file sizes (as long values expressing the file size in bytes). Their usage is discouraged and is currently included only for legacy compatibility:

// using NeoSmart.PrettySize
var untypedSize = 1 * EXABYTE;
var formatted = PrettySize.Format(untypedSize, UnitBase.Base10);
Console.WriteLine(formatted); // Prints "1.00 EB"

License and Credits

PrettySize was developed by Mahmoud Al-Qudsi of NeoSmart Technologies and is released under the terms of the MIT public license. The name "PrettySize" is copyright NeoSmart Technologies 2017 and may not be used without permission (fair use is excluded).

Product Versions
.NET net5.0 net5.0-windows net6.0 net6.0-android net6.0-ios net6.0-maccatalyst net6.0-macos net6.0-tvos net6.0-windows
.NET Core netcoreapp1.0 netcoreapp1.1 netcoreapp2.0 netcoreapp2.1 netcoreapp2.2 netcoreapp3.0 netcoreapp3.1
.NET Standard netstandard1.3 netstandard1.4 netstandard1.5 netstandard1.6 netstandard2.0 netstandard2.1
.NET Framework net20 net35 net40 net403 net45 net451 net452 net46 net461 net462 net463 net47 net471 net472 net48
MonoAndroid monoandroid
MonoMac monomac
MonoTouch monotouch
Tizen tizen30 tizen40 tizen60
Universal Windows Platform uap uap10.0
Xamarin.iOS xamarinios
Xamarin.Mac xamarinmac
Xamarin.TVOS xamarintvos
Xamarin.WatchOS xamarinwatchos
Compatible target framework(s)
Additional computed target framework(s)
Learn more about Target Frameworks and .NET Standard.
  • .NETFramework 2.0

    • No dependencies.
  • .NETFramework 4.0

    • No dependencies.
  • .NETStandard 1.3

  • .NETStandard 2.1

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
3.1.1 94 6/27/2022
3.1.0 60 6/26/2022
3.0.0 69 6/18/2022
2.1.0 3,521 6/11/2017
2.0.0 765 6/11/2017
1.0.0 769 6/10/2017