| name | humanizer |
| description | Guidance for Humanizer library for .NET string, date, number, and enum formatting. USE FOR: human-readable date/time formatting, pluralization, number-to-words conversion, enum display names, truncation, byte size formatting, casing transformations. DO NOT USE FOR: localization infrastructure (use IStringLocalizer), parsing human input back to types, business logic, data storage formatting.
|
| license | MIT |
| metadata | {"displayName":"Humanizer","author":"Tyler-R-Kendrick","version":"1.0.0"} |
| compatibility | ["claude","copilot","cursor"] |
| references | [{"title":"Humanizer GitHub Repository","url":"https://github.com/Humanizr/Humanizer"},{"title":"Humanizer NuGet Package","url":"https://www.nuget.org/packages/Humanizer"},{"title":"Humanizer.Core NuGet Package","url":"https://www.nuget.org/packages/Humanizer.Core"}] |
Humanizer
Overview
Humanizer is a .NET library that manipulates and displays strings, enums, dates, times, timespans, numbers, and quantities in a human-friendly format. It provides extension methods that transform programmatic values into natural language and vice versa. Humanizer supports over 40 languages for localized output.
The library is designed for presentation layers where raw data values need to be displayed as readable text. It converts DateTime differences into phrases like "3 hours ago", numbers into words, enums into display-friendly strings, and byte counts into formatted sizes.
Install via NuGet:
dotnet add package Humanizer
Date and Time Humanization
Transform DateTime and TimeSpan values into human-readable relative time strings.
using System;
using Humanizer;
var pastDate = DateTime.UtcNow.AddHours(-3);
Console.WriteLine(pastDate.Humanize());
var futureDate = DateTime.UtcNow.AddDays(2);
Console.WriteLine(futureDate.Humanize());
var duration = TimeSpan.FromMinutes(187);
Console.WriteLine(duration.Humanize());
Console.WriteLine(duration.Humanize(precision: 2));
var uptime = TimeSpan.FromSeconds(93784);
Console.WriteLine(uptime.Humanize(precision: 3));
var offset = DateTimeOffset.UtcNow.AddMinutes(-45);
Console.WriteLine(offset.Humanize());
String Transformations
Humanizer provides extensive string manipulation including casing, truncation, and word transformations.
using Humanizer;
Console.WriteLine("PascalCaseProperty".Humanize());
Console.WriteLine("some_database_column".Humanize());
Console.WriteLine("HTML parser".Humanize());
Console.WriteLine("the quick brown fox".Pascalize());
Console.WriteLine("the quick brown fox".Camelize());
Console.WriteLine("the quick brown fox".Underscore());
Console.WriteLine("the quick brown fox".Kebaberize());
Console.WriteLine("the quick brown fox".Transform(To.TitleCase));
Console.WriteLine("the quick brown fox".Transform(To.SentenceCase));
Console.WriteLine("Long descriptive text here".Truncate(15));
Console.WriteLine("Long descriptive text here".Truncate(15, Truncator.FixedNumberOfWords));
Console.WriteLine("Long text here".Truncate(10, "---"));
Pluralization and Singularization
Convert between singular and plural forms of English words.
using Humanizer;
Console.WriteLine("person".Pluralize());
Console.WriteLine("mouse".Pluralize());
Console.WriteLine("criterion".Pluralize());
Console.WriteLine("octopus".Pluralize());
Console.WriteLine("people".Singularize());
Console.WriteLine("mice".Singularize());
Console.WriteLine("categories".Singularize());
Console.WriteLine("item".ToQuantity(0));
Console.WriteLine("item".ToQuantity(1));
Console.WriteLine("item".ToQuantity(5));
Console.WriteLine("item".ToQuantity(5, ShowQuantityAs.Words));
Number to Words and Ordinals
Convert numeric values to their word representations and ordinal forms.
using Humanizer;
Console.WriteLine(42.ToWords());
Console.WriteLine(1234.ToWords());
Console.WriteLine(1.Ordinalize());
Console.WriteLine(2.Ordinalize());
Console.WriteLine(3.Ordinalize());
Console.WriteLine(11.Ordinalize());
Console.WriteLine(21.Ordinalize());
Console.WriteLine(1.ToOrdinalWords());
Console.WriteLine(21.ToOrdinalWords());
Console.WriteLine(2025.ToRoman());
Console.WriteLine("XIV".FromRoman());
Enum Humanization
Display enum values as human-readable strings using [Description] attributes or automatic PascalCase splitting.
using System.ComponentModel;
using Humanizer;
public enum OrderStatus
{
[Description("Awaiting Payment")]
PendingPayment,
[Description("Being Processed")]
InProcessing,
Shipped,
[Description("Delivered to Customer")]
Delivered,
ReturnRequested
}
Console.WriteLine(OrderStatus.PendingPayment.Humanize());
Console.WriteLine(OrderStatus.InProcessing.Humanize());
Console.WriteLine(OrderStatus.Shipped.Humanize());
Console.WriteLine(OrderStatus.ReturnRequested.Humanize());
var status = "Awaiting Payment".DehumanizeTo<OrderStatus>();
Byte Size Formatting
Format byte counts into human-readable file sizes.
using Humanizer;
using Humanizer.Bytes;
var fileSize = ByteSize.FromBytes(1548576);
Console.WriteLine(fileSize.ToString());
Console.WriteLine(fileSize.Humanize());
Console.WriteLine(ByteSize.FromKilobytes(512).ToString());
Console.WriteLine(ByteSize.FromGigabytes(2.5).ToString());
var total = ByteSize.FromMegabytes(100) + ByteSize.FromMegabytes(250);
Console.WriteLine(total.Humanize());
Console.WriteLine(ByteSize.FromMegabytes(10).Per(TimeSpan.FromSeconds(1)).Humanize());
Localization
Humanizer supports localized output by setting the current culture.
using System.Globalization;
using Humanizer;
var deCulture = new CultureInfo("de-DE");
Console.WriteLine(DateTime.UtcNow.AddHours(-3).Humanize(culture: deCulture));
var frCulture = new CultureInfo("fr-FR");
Console.WriteLine(42.ToWords(frCulture));
Console.WriteLine(DateTime.UtcNow.AddDays(-1).Humanize(culture: frCulture));
Best Practices
- Use Humanizer only at presentation boundaries (UI, API responses, notifications) -- never store humanized strings in databases or use them for logic.
- Cache compiled templates or precomputed humanized values when rendering lists, since calling
.Humanize() in tight loops on thousands of items adds measurable overhead.
- Pass
CultureInfo explicitly when localizing output rather than relying on Thread.CurrentThread.CurrentCulture, which can change unexpectedly in async code.
- Use
.ToQuantity() for count-dependent labels like "3 items" instead of manually concatenating count + pluralized noun.
- Annotate enums with
[Description] for display text that differs from the member name, and rely on .Humanize() for simple PascalCase splitting.
- Prefer
.Truncate() with a word-based truncator for user-facing text to avoid cutting words in half.
- Use
ByteSize for file and bandwidth formatting instead of manual division-by-1024 logic, which is error-prone and inconsistent.
- Specify precision in
TimeSpan.Humanize(precision: N) to control how many time units appear (e.g., "2 hours, 30 minutes" vs. "2 hours").
- Avoid
.Humanize() on untrusted user input strings -- Humanizer assumes well-formed PascalCase or snake_case identifiers and may produce unexpected output on arbitrary text.
- Use
.Dehumanize() and .DehumanizeTo<T>() only for round-tripping display strings back to enums or known values, not for parsing arbitrary user input.