Skip to content

Humanizer

Developer UtilitiesUtilitiesC#

What it is

Humanizer converts values into human-readable forms — relative dates, pluralisation, number words, byte sizes and cased strings.

Extension methods on primitives and strings. Most support localisation through the current culture.

Installation

dotnet add package Humanizer

Getting started

The smallest useful thing you can do with it, and what each part means.

The everyday conversions
DateTime.UtcNow.AddHours(-2).Humanize();      // "2 hours ago"
TimeSpan.FromDays(400).Humanize(2);           // "1 year, 1 month"

"book".ToQuantity(1);                          // "1 book"
"book".ToQuantity(5);                          // "5 books"
"person".Pluralize();                          // "people" (irregulars handled)

(1024L * 1024 * 3).Bytes().Humanize("#.##");  // "3 MB"

42.ToWords();                                  // "forty-two"
3.ToOrdinalWords();                            // "third"

"PascalCaseString".Humanize();                 // "Pascal case string"
"some_property_name".Pascalize();              // "SomePropertyName"
ToQuantity is the one that earns its place: it pluralises and prefixes the count in one call, handling irregular nouns that a naive `+ "s"` gets wrong.

Advanced usage

Where the library earns its place over a simpler alternative.

Localisation and enum descriptions
using (CultureInfo.CurrentCulture = new CultureInfo("de"))
{
    DateTime.UtcNow.AddDays(-1).Humanize();   // "vor einem Tag"
}

public enum OrderStatus
{
    [Description("Awaiting payment")] AwaitingPayment,
    PartiallyShipped,
}

OrderStatus.AwaitingPayment.Humanize();    // "Awaiting payment" (attribute)
OrderStatus.PartiallyShipped.Humanize();   // "Partially shipped" (inferred)
Enum humanisation removes the switch statement that usually maps enum values to display strings, and the Description attribute overrides it where the inferred text is wrong.

Errors and fixes

The failures you are most likely to hit, and what actually resolves them.

Output is in the wrong language
It follows CurrentUICulture. Set it per request from the Accept-Language header.
A domain term pluralises incorrectly
Register the exception with Vocabularies.Default.AddIrregular or AddUncountable at startup.

Best practices

  • Use ToQuantity rather than concatenating a count with a manually pluralised noun.
  • Set the culture explicitly when output must match the user's locale, not the server's.
  • Use it for display only; never parse humanised output back into a value.
  • Prefer the Description attribute over a switch for enum display names.

Background

Why it exists, and what it was reacting to.

Humanizer collects the small formatting utilities every application reimplements badly: "2 hours ago", "3 items", "1.4 MB". Doing them correctly across locales is more involved than it looks.