Ookii.BinarySize 1.1.0

There is a newer prerelease version of this package available.
See the version list below for details.
dotnet add package Ookii.BinarySize --version 1.1.0                
NuGet\Install-Package Ookii.BinarySize -Version 1.1.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Ookii.BinarySize" Version="1.1.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Ookii.BinarySize --version 1.1.0                
#r "nuget: Ookii.BinarySize, 1.1.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Ookii.BinarySize as a Cake Addin
#addin nuget:?package=Ookii.BinarySize&version=1.1.0

// Install Ookii.BinarySize as a Cake Tool
#tool nuget:?package=Ookii.BinarySize&version=1.1.0                

Ookii.BinarySize

Ookii.BinarySize is a modern library for parsing and displaying quantities of bytes, using human-readable representation.

It provides functionality to parse numeric values that end with a multiple-byte unit, such as B, KB, MiB, kilobyte, mebibytes, and so on, and to format them for display in the same way. It can automatically choose the best unit, or you can choose the one you want, based on the format string.

  • Supports units with SI prefixes ("KB", "MB", "GB", "TB", "PB", and "EB"), and IEC prefixes ("KiB", "MiB", "GiB", "TiB", "PiB", and "EiB"), with and without the "B".
  • Also supports unabbreviated units for both parsing and formatting: "byte", "kilobyte", "megabyte", "gigabyte", "terabyte", "petabyte", and "exabyte", and their IEC equivalents "kibibyte", "mebibyte", "gibibyte", "tebibyte", "pebibyte", and "exbibyte".
  • Interpret SI prefixes as either powers of two or powers of ten.
  • Parse and store values up to approximately positive and negative 8 EiB, using Int64 (long) as the underlying storage.
  • Support for localizing units and prefixes.
  • Implements arithmetic and binary operators, and supports .Net 7 generic math.
  • Trim-friendly.

Besides display formatting and parsing user input, BinarySize provides everything needed to easily use human-readable byte sizes in places such as configuration files, serialized XML and JSON, and command line arguments.

You can format values using custom format strings to pick a unit, or choose one automatically, using either powers of ten or powers of two for SI units.

var value = BinarySize.FromGibi(2.5);
Console.WriteLine($"{value: B} is equal to:");
Console.WriteLine($"Default formatting: {value}");
Console.WriteLine($"Automatic formatting: {value: AB}");
Console.WriteLine($"Shortest formatting: {value:#.0 SiB}");
Console.WriteLine($"Explicit formatting: {value:#,###Ki}");
Console.WriteLine($"Unabbreviated formatting: {value:#,### Sibyte}");
Console.WriteLine($"Automatic formatting (decimal): {value: aB}");
Console.WriteLine($"Shortest formatting (decimal): {value:#.0 sB}");
Console.WriteLine($"Explicit formatting (decimal): {value:#,###k}");
Console.WriteLine($"Unabbreviated formatting (decimal): {value:#,### sbyte}");

Which outputs:

2684354560 B is equal to:
Default formatting: 2560 MiB
Automatic formatting: 2560 MB
Shortest formatting: 2.5 GiB
Explicit formatting: 2,621,440Ki
Unabbreviated formatting: 2.5 gibibytes
Automatic formatting (decimal): 2684354560 B
Shortest formatting (decimal): 2.7 GB
Explicit formatting (decimal): 2,684,355k
Unabbreviated formatting (decimal): 2.7 gigabytes

See the documentation for BinarySize.ToString() for information on the format string.

You can also parse values that contain any supported unit.

var values = new[] { "100", "100B", "10 KB", "2.5 MiB", "5G" };
foreach (var value in values)
{
    var size = BinarySize.Parse(value, CultureInfo.InvariantCulture);
    Console.WriteLine($"'{value}' == {size.Value} bytes");
}

Which outputs:

'100' == 100 bytes
'100B' == 100 bytes
'10 KB' == 10240 bytes
'2.5 MiB' == 2621440 bytes
'5G' == 5368709120 bytes

See the GitHub project for more information.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 is compatible. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (2)

Showing the top 2 NuGet packages that depend on Ookii.BinarySize:

Package Downloads
Ookii.BinarySize.Async

Provides extension methods for IAsyncEnumerable<BinarySize> for use with Ookii.BinarySize.

Ookii.AnswerFile

A library for generating answer files (unattend.xml and autounattend.xml) for unattended Windows installation.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.2.0-preview 84 10/31/2024
1.1.0 901 9/15/2023
1.1.0-preview 519 8/29/2023
1.0.0 675 7/31/2023
1.0.0-preview2 612 7/24/2023
1.0.0-preview 618 7/24/2023