Philiprehberger.FileSizeHumanizer 0.2.7

dotnet add package Philiprehberger.FileSizeHumanizer --version 0.2.7
                    
NuGet\Install-Package Philiprehberger.FileSizeHumanizer -Version 0.2.7
                    
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="Philiprehberger.FileSizeHumanizer" Version="0.2.7" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Philiprehberger.FileSizeHumanizer" Version="0.2.7" />
                    
Directory.Packages.props
<PackageReference Include="Philiprehberger.FileSizeHumanizer" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Philiprehberger.FileSizeHumanizer --version 0.2.7
                    
#r "nuget: Philiprehberger.FileSizeHumanizer, 0.2.7"
                    
#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.
#:package Philiprehberger.FileSizeHumanizer@0.2.7
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Philiprehberger.FileSizeHumanizer&version=0.2.7
                    
Install as a Cake Addin
#tool nuget:?package=Philiprehberger.FileSizeHumanizer&version=0.2.7
                    
Install as a Cake Tool

Philiprehberger.FileSizeHumanizer

CI NuGet Last updated

Convert byte counts into human-readable file size strings and parse them back to bytes.

Installation

dotnet add package Philiprehberger.FileSizeHumanizer

Usage

Humanize (legacy units)

using Philiprehberger.FileSizeHumanizer;

FileSize.Humanize(0);              // "0 B"
FileSize.Humanize(512);            // "512 B"
FileSize.Humanize(1024);           // "1.0 KB"
FileSize.Humanize(1_536);          // "1.5 KB"
FileSize.Humanize(3_355_443);      // "3.2 MB"
FileSize.Humanize(1_073_741_824);  // "1.0 GB"
FileSize.Humanize(2_199_023_255_552L); // "2.0 TB"

// Control decimal places
FileSize.Humanize(1_500_000, decimals: 2); // "1.43 MB"
FileSize.Humanize(1_500_000, decimals: 0); // "1 MB"

SI and Binary (IEC) standards

// Binary (IEC) — base 1024, units: B, KiB, MiB, GiB, TiB, PiB, EiB
FileSize.Humanize(1024, standard: SizeStandard.Binary);        // "1.0 KiB"
FileSize.Humanize(1_048_576, standard: SizeStandard.Binary);   // "1.0 MiB"

// SI — base 1000, units: B, KB, MB, GB, TB, PB, EB
FileSize.Humanize(1000, standard: SizeStandard.SI);            // "1.0 KB"
FileSize.Humanize(1_000_000, standard: SizeStandard.SI);       // "1.0 MB"

// With decimal control
FileSize.Humanize(1_500_000, decimals: 2, standard: SizeStandard.SI); // "1.50 MB"

Parse and TryParse

// Parse strings back to bytes (supports both SI and IEC units)
FileSize.Parse("3.2 MB");   // 3200000
FileSize.Parse("1.5 GiB");  // 1610612736
FileSize.Parse("1024 B");   // 1024

// Whitespace between number and unit is handled
FileSize.Parse("3.2MB");    // 3200000

// TryParse — non-throwing variant
if (FileSize.TryParse("2.5 GB", out var bytes))
{
    Console.WriteLine(bytes); // 2500000000
}

// Returns false for invalid input
FileSize.TryParse("not a size", out _); // false

API

SizeStandard enum

Value Base Units
Binary 1024 B, KiB, MiB, GiB, TiB, PiB, EiB
SI 1000 B, KB, MB, GB, TB, PB, EB

FileSize

Method Description
Humanize(long bytes, int decimals = 1) Convert a byte count to a human-readable string using binary (1024) scale with legacy unit names (KB, MB, etc.)
Humanize(long bytes, int decimals = 1, SizeStandard standard = SizeStandard.Binary) Convert a byte count using the specified standard with proper IEC or SI unit names
Parse(string input) Parse a human-readable size string back to bytes. Throws FormatException on invalid input.
TryParse(string input, out long bytes) Non-throwing variant of Parse. Returns true on success.

Development

dotnet build src/Philiprehberger.FileSizeHumanizer.csproj --configuration Release

Support

If you find this project useful:

Star the repo

🐛 Report issues

💡 Suggest features

❤️ Sponsor development

🌐 All Open Source Projects

💻 GitHub Profile

🔗 LinkedIn Profile

License

MIT

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  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.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net8.0

    • 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
0.2.7 116 4/1/2026
0.2.6 110 3/25/2026
0.2.5 104 3/23/2026
0.2.4 101 3/21/2026
0.2.3 116 3/16/2026
0.2.2 105 3/16/2026
0.2.1 111 3/16/2026
0.2.0 111 3/13/2026
0.1.1 119 3/11/2026
0.1.0 113 3/11/2026