Skip to content

Latest commit

 

History

History
88 lines (65 loc) · 3.44 KB

File metadata and controls

88 lines (65 loc) · 3.44 KB

Humanizer

Build Status NuGet version NuGet downloads

Humanizer is a .NET library for turning strings, enums, dates, times, durations, numbers, quantities, and collections into human-friendly text.

Install

dotnet add package Humanizer

Select your Humanizer version in the documentation site for version-correct package, framework, and API guidance.

Example

using System.Globalization;
using Humanizer;

var culture = CultureInfo.GetCultureInfo("en-US");
var text = TimeSpan.FromMinutes(2).Humanize(culture: culture);
var indianNumber = 1_000_000_000L.ToIndianWords(IndianScaleStyle.CroreBased);
var precise = TimeSpan.FromMilliseconds(1500).HumanizeWithFractionalSeconds(
    precision: 1,
    maxFractionalDigits: 3,
    roundingMode: MidpointRounding.ToEven,
    culture: culture,
    maxUnit: TimeUnit.Second);

Console.WriteLine(text); // 2 minutes
Console.WriteLine(indianNumber); // one hundred crore
Console.WriteLine(precise); // 1.5 seconds

var german = CultureInfo.GetCultureInfo("de-DE");
var duration = TimeSpan.FromDays(7).HumanizeWithCase(
    GrammaticalCase.Dative,
    culture: german);

Console.WriteLine($"in {duration}"); // in einer Woche

In v4 previews, applications can provide the exact noun forms for a culture's CLDR cardinal categories:

var files = new CardinalInflectionForms(
    lemma: "plik",
    other: "pliku",
    few: "pliki",
    many: "plików");

if (files.TryInflect(5m, CultureInfo.GetCultureInfo("pl"), out var noun))
    Console.WriteLine(noun); // plików

TryInflect returns false when the selected category has no authored form; it does not guess localized morphology or fall back to English. See inflection and quantities for the exact-form and lemmatization contracts.

HumanizeWithCase returns only the duration phrase; add any required preposition yourself. Singular forms may include a locale-authored one-word or article, and a locale may encode a count in the unit form. Locales and custom components without verified case support throw NotSupportedException instead of falling back to English.

Documentation

Repository

The main library is in src/Humanizer, and its xUnit tests are in tests/Humanizer.Tests. Read the contribution guide before sending a change.

Humanizer is available under the MIT license.

Support