Humanizer C# (How It Works For Developers)
Humanizer is a powerful and flexible .NET library that simplifies and humanizes the process of working with data, especially when it comes to displaying information in a user-friendly format. Whether you need to convert dates to relative time strings ("3 days ago"), pluralize words, format numbers as words, or work with enums, displaying strings, Pascal case input strings as sentences with custom descriptions, underscored input strings to normal title case strings, and long text truncation, Humanizer provides a plethora of tools and extension methods to handle these tasks elegantly in C#.NET to convert dehumanized input strings into sentences.
In this article, we will discuss a detailed tutorial of Humanizer in C#. We will also discuss how to generate PDF documents using Humanizer and IronPDF for the C# PDF Library.
Setting Up Humanizer in C#
To get started with Humanizer, you need to install the library via NuGet. In your project, you can do this through the Package Manager Console with the following command:
Install-Package Humanizer
Alternatively, if you are using the .NET Core CLI, you can add Humanizer with:
dotnet add package Humanizer
After installation, you can start using Humanizer by including the appropriate namespace in your C# files:
using Humanizer;using Humanizer;Humanizing Dates and Times
One of the most common uses of Humanizer is to convert dates and times into human-readable formats, timespans, numbers, and quantities using the Humanize method. This is particularly useful for displaying relative times, such as "2 hours ago" or "in 5 days".
Example: Humanizing Relative Time
using System;
class HumanizerDemo
{
static void Main()
{
DateTime pastDate = DateTime.Now.AddDays(-3);
// Humanize the past date, which converts it to a relative time format
string humanizedTime = pastDate.Humanize(); // Output: "3 days ago"
DateTime futureDate = DateTime.Now.AddHours(5);
// Humanize the future date, presenting it in relative time
string futureHumanizedTime = futureDate.Humanize(); // Output: "in 5 hours"
Console.WriteLine("Humanized Past Date: " + humanizedTime);
Console.WriteLine("Humanized Future Date: " + futureHumanizedTime);
}
}using System;
class HumanizerDemo
{
static void Main()
{
DateTime pastDate = DateTime.Now.AddDays(-3);
// Humanize the past date, which converts it to a relative time format
string humanizedTime = pastDate.Humanize(); // Output: "3 days ago"
DateTime futureDate = DateTime.Now.AddHours(5);
// Humanize the future date, presenting it in relative time
string futureHumanizedTime = futureDate.Humanize(); // Output: "in 5 hours"
Console.WriteLine("Humanized Past Date: " + humanizedTime);
Console.WriteLine("Humanized Future Date: " + futureHumanizedTime);
}
}The Humanizer extension method automatically handles different time units and even adjusts for grammatical correctness.

Humanizing TimeSpans
Humanizer can also humanize TimeSpan objects, making it easy to display durations in a readable format.
Example: Humanizing TimeSpan
using System;
class TimeSpanHumanizerDemo
{
static void Main()
{
TimeSpan timeSpan = TimeSpan.FromMinutes(123);
// Humanizing the TimeSpan into hours and minutes
string humanizedTimeSpan = timeSpan.Humanize(2); // Output: "2 hours, 3 minutes"
Console.WriteLine("Humanized TimeSpan: " + humanizedTimeSpan);
}
}using System;
class TimeSpanHumanizerDemo
{
static void Main()
{
TimeSpan timeSpan = TimeSpan.FromMinutes(123);
// Humanizing the TimeSpan into hours and minutes
string humanizedTimeSpan = timeSpan.Humanize(2); // Output: "2 hours, 3 minutes"
Console.WriteLine("Humanized TimeSpan: " + humanizedTimeSpan);
}
}
Working with Numbers
Humanizer provides several methods to convert numbers into human-readable words and to handle ordinal numbers.
Example: Converting Numbers to Words
using System;
class NumberHumanizerDemo
{
static void Main()
{
int number = 123;
// Convert number to words
string words = number.ToWords(); // Output: "one hundred and twenty-three"
Console.WriteLine("Number in Words: " + words);
}
}using System;
class NumberHumanizerDemo
{
static void Main()
{
int number = 123;
// Convert number to words
string words = number.ToWords(); // Output: "one hundred and twenty-three"
Console.WriteLine("Number in Words: " + words);
}
}
Example: Converting Numbers to Ordinals
using System;
class OrdinalHumanizerDemo
{
static void Main()
{
int number = 21;
// Convert number to ordinal words
string ordinal = number.ToOrdinalWords(); // Output: "twenty-first"
Console.WriteLine("Ordinal Number: " + ordinal);
}
}using System;
class OrdinalHumanizerDemo
{
static void Main()
{
int number = 21;
// Convert number to ordinal words
string ordinal = number.ToOrdinalWords(); // Output: "twenty-first"
Console.WriteLine("Ordinal Number: " + ordinal);
}
}
Pluralization and Singularization
Humanizer makes it easy to convert words between their singular and plural forms, which is useful for dynamically generating long text based on quantity.
Example: Pluralizing and Singularizing Words
using System;
class PluralizationDemo
{
static void Main()
{
string singular = "car";
// Pluralize the word
string plural = singular.Pluralize(); // Output: "cars"
string word = "people";
// Singularize the word
string singularForm = word.Singularize(); // Output: "person"
Console.WriteLine("Plural of 'car': " + plural);
Console.WriteLine("Singular of 'people': " + singularForm);
}
}using System;
class PluralizationDemo
{
static void Main()
{
string singular = "car";
// Pluralize the word
string plural = singular.Pluralize(); // Output: "cars"
string word = "people";
// Singularize the word
string singularForm = word.Singularize(); // Output: "person"
Console.WriteLine("Plural of 'car': " + plural);
Console.WriteLine("Singular of 'people': " + singularForm);
}
}Humanizer handles irregular pluralizations and singularizations as well, making it robust for various use cases.

Formatting Enums
Enums are frequently used in C# applications to represent a set of named constants. Humanizer can convert enum values to human-readable strings.
Example: Humanizing Enums
using System;
public enum MyEnum
{
FirstValue,
SecondValue
}
class EnumHumanizerDemo
{
static void Main()
{
MyEnum enumValue = MyEnum.FirstValue;
// Humanizing enum to a readable format
string humanizedEnum = enumValue.Humanize(); // Output: "First value"
Console.WriteLine("Humanized Enum: " + humanizedEnum);
}
}using System;
public enum MyEnum
{
FirstValue,
SecondValue
}
class EnumHumanizerDemo
{
static void Main()
{
MyEnum enumValue = MyEnum.FirstValue;
// Humanizing enum to a readable format
string humanizedEnum = enumValue.Humanize(); // Output: "First value"
Console.WriteLine("Humanized Enum: " + humanizedEnum);
}
}This method can be particularly useful for displaying user-friendly labels in UIs.

Humanizing Byte Sizes
Another handy feature of Humanizer is the ability to humanize byte sizes, converting large byte values into readable formats like KB, MB, or GB.
Example: Humanizing Byte Sizes
using System;
class ByteSizeHumanizerDemo
{
static void Main()
{
long bytes = 1048576;
// Humanize bytes to a readable size format
string humanizedBytes = bytes.Bytes().Humanize(); // Output: "1 MB"
Console.WriteLine("Humanized Byte Size: " + humanizedBytes);
}
}using System;
class ByteSizeHumanizerDemo
{
static void Main()
{
long bytes = 1048576;
// Humanize bytes to a readable size format
string humanizedBytes = bytes.Bytes().Humanize(); // Output: "1 MB"
Console.WriteLine("Humanized Byte Size: " + humanizedBytes);
}
}
Advanced Scenarios
Humanizer is not limited to the basic scenarios described above. It supports a wide range of advanced features such as the Truncate method and multiple languages and extensions.
Example: Humanizing DateTime Offsets
Humanizer can also handle DateTimeOffset, which is useful for applications dealing with time zones.
using System;
class DateTimeOffsetHumanizerDemo
{
static void Main()
{
DateTimeOffset dateTimeOffset = DateTimeOffset.Now.AddDays(-2);
// Humanize DateTimeOffset
string humanizedDateTimeOffset = dateTimeOffset.Humanize(); // Output: "2 days ago"
Console.WriteLine("Humanized DateTimeOffset: " + humanizedDateTimeOffset);
}
}using System;
class DateTimeOffsetHumanizerDemo
{
static void Main()
{
DateTimeOffset dateTimeOffset = DateTimeOffset.Now.AddDays(-2);
// Humanize DateTimeOffset
string humanizedDateTimeOffset = dateTimeOffset.Humanize(); // Output: "2 days ago"
Console.WriteLine("Humanized DateTimeOffset: " + humanizedDateTimeOffset);
}
}
Performance Considerations
Humanizer is designed to be efficient, but like any library, its performance depends on how it is used. For applications requiring high performance, especially those dealing with large datasets or real-time processing, it is essential to consider the impact of frequent humanization operations.
IronPDF for C#
IronPDF is a comprehensive PDF generation and manipulation library for .NET applications. It enables developers to create, read, edit, and extract content from PDF files with ease. IronPDF is designed to be user-friendly, offering a wide range of functionalities including converting HTML to PDF, merging documents, adding watermarks, and much more. Its versatility and powerful features make it an excellent choice for handling PDF documents in C# projects.
Installing IronPDF via NuGet Package Manager
Follow these steps to install IronPDF using the NuGet Package Manager:
Open Your Project in Visual Studio:
- Launch Visual Studio and open your existing C# project or create a new one.
Open the NuGet Package Manager:
- Right-click on your project in the Solution Explorer.
- Select "Manage NuGet Packages…" from the context menu.

Install IronPDF:
- In the NuGet Package Manager, go to the "Browse" tab.
- Search for IronPDF.
- Select the IronPDF package from the search results.
- Click the "Install" button to add IronPDF to your project.

By following these steps, IronPDF will be installed and ready to use in your C# project, allowing you to leverage its powerful PDF manipulation capabilities.
C# Humanizer and IronPDF Code Example
using Humanizer;
using IronPdf;
using System;
using System.Collections.Generic;
class PDFGenerationDemo
{
static void Main()
{
// Instantiate the PDF renderer
var renderer = new ChromePdfRenderer();
// Generate humanized content
List<string> content = GenerateHumanizedContent();
// HTML content template for the PDF
string htmlContent = "<h1>Humanizer Examples</h1><ul>";
// Build the list items to add to the HTML content
foreach (var item in content)
{
htmlContent += $"<li>{item}</li>";
}
htmlContent += "</ul>";
// Render the HTML into a PDF document
var pdf = renderer.RenderHtmlAsPdf(htmlContent);
// Save the PDF to a file
pdf.SaveAs("output.pdf");
Console.WriteLine("PDF document generated successfully: output.pdf");
}
/// <summary>
/// Generates a list of humanized content examples
/// </summary>
/// <returns>List of humanized content as strings</returns>
static List<string> GenerateHumanizedContent()
{
List<string> content = new List<string>();
// DateTime examples
DateTime pastDate = DateTime.Now.AddDays(-3);
DateTime futureDate = DateTime.Now.AddHours(5);
content.Add($"DateTime.Now: {DateTime.Now}");
content.Add($"3 days ago: {pastDate.Humanize()}");
content.Add($"In 5 hours: {futureDate.Humanize()}");
// TimeSpan examples
TimeSpan timeSpan = TimeSpan.FromMinutes(123);
content.Add($"TimeSpan of 123 minutes: {timeSpan.Humanize()}");
// Number examples
int number = 12345;
content.Add($"Number 12345 in words: {number.ToWords()}");
content.Add($"Ordinal of 21: {21.ToOrdinalWords()}");
// Pluralization examples
string singular = "car";
content.Add($"Plural of 'car': {singular.Pluralize()}");
string plural = "children";
content.Add($"Singular of 'children': {plural.Singularize()}");
// Byte size examples
long bytes = 1048576;
content.Add($"1,048,576 bytes: {bytes.Bytes().Humanize()}");
return content;
}
}using Humanizer;
using IronPdf;
using System;
using System.Collections.Generic;
class PDFGenerationDemo
{
static void Main()
{
// Instantiate the PDF renderer
var renderer = new ChromePdfRenderer();
// Generate humanized content
List<string> content = GenerateHumanizedContent();
// HTML content template for the PDF
string htmlContent = "<h1>Humanizer Examples</h1><ul>";
// Build the list items to add to the HTML content
foreach (var item in content)
{
htmlContent += $"<li>{item}</li>";
}
htmlContent += "</ul>";
// Render the HTML into a PDF document
var pdf = renderer.RenderHtmlAsPdf(htmlContent);
// Save the PDF to a file
pdf.SaveAs("output.pdf");
Console.WriteLine("PDF document generated successfully: output.pdf");
}
/// <summary>
/// Generates a list of humanized content examples
/// </summary>
/// <returns>List of humanized content as strings</returns>
static List<string> GenerateHumanizedContent()
{
List<string> content = new List<string>();
// DateTime examples
DateTime pastDate = DateTime.Now.AddDays(-3);
DateTime futureDate = DateTime.Now.AddHours(5);
content.Add($"DateTime.Now: {DateTime.Now}");
content.Add($"3 days ago: {pastDate.Humanize()}");
content.Add($"In 5 hours: {futureDate.Humanize()}");
// TimeSpan examples
TimeSpan timeSpan = TimeSpan.FromMinutes(123);
content.Add($"TimeSpan of 123 minutes: {timeSpan.Humanize()}");
// Number examples
int number = 12345;
content.Add($"Number 12345 in words: {number.ToWords()}");
content.Add($"Ordinal of 21: {21.ToOrdinalWords()}");
// Pluralization examples
string singular = "car";
content.Add($"Plural of 'car': {singular.Pluralize()}");
string plural = "children";
content.Add($"Singular of 'children': {plural.Singularize()}");
// Byte size examples
long bytes = 1048576;
content.Add($"1,048,576 bytes: {bytes.Bytes().Humanize()}");
return content;
}
}
Conclusion
Humanizer is an indispensable library for .NET developers aiming to create applications that present information in a user-friendly and human-readable format. Its wide range of features, from date and time humanization to number and enum formatting, make it a versatile tool for improving the usability of applications. By leveraging Humanizer, developers can save time and effort in implementing custom formatting logic, ensuring that their applications communicate data more effectively to end-users.
Similarly, IronPDF offers comprehensive PDF generation and manipulation capabilities, making it an excellent choice for creating and handling PDF documents in C# projects. Together, Humanizer and IronPDF can significantly enhance the functionality and presentation of .NET applications. For more details on IronPDF licensing, refer to the IronPDF Licensing Information. To explore further, check out our Detailed Tutorial on HTML to PDF Conversion.
Frequently Asked Questions
What is the purpose of the Humanizer library in C#?
The Humanizer library in C# is designed to transform data into human-friendly formats, such as converting dates to relative time strings, pluralizing words, formatting numbers as words, and handling enums. It helps developers present data in a more readable and accessible way.
How can I convert a DateTime to a relative time string in C#?
You can use Humanizer's Humanize method to convert a DateTime object into a relative time string, such as '3 days ago' or 'in 5 hours'.
How do I install the Humanizer library in a C# project?
To install the Humanizer library in a C# project, you can use the NuGet Package Manager Console with the command Install-Package Humanizer or use the .NET Core CLI with dotnet add package Humanizer.
What are some examples of data transformations possible with Humanizer?
Humanizer can perform several data transformations, such as converting Pascal case strings to sentences, transforming underscored strings to title case, and truncating long text to a specified length.
Can Humanizer help with pluralization of words in C#?
Yes, Humanizer provides methods to pluralize and singularize words, effectively handling both regular and irregular forms, such as converting 'car' to 'cars' or 'people' to 'person'.
How does Humanizer handle enums in C#?
Humanizer can convert enum values to human-readable strings, making it easier to display user-friendly labels in interfaces.
What features does a C# PDF library offer?
A C# PDF library like IronPDF offers features such as creating, reading, editing, and extracting content from PDF files. It can also convert HTML to PDF, merge documents, and add watermarks.
How do I install a C# PDF library in my project?
To install a C# PDF library, you can use the NuGet Package Manager by searching for the library name, such as IronPDF, in the 'Browse' tab and clicking 'Install'.
What are the benefits of combining Humanizer with a PDF library in C#?
By combining Humanizer with a PDF library like IronPDF, developers can generate human-readable content with Humanizer and then render it into a PDF document, facilitating the creation of user-friendly PDF reports and documentation.
What should I consider regarding performance when using Humanizer?
While Humanizer is designed to be efficient, developers should consider the impact of frequent humanization operations in applications that require high performance with large datasets or real-time processing.









