Natural Data Formatting in.NET using Humanizer (NuGet)

As a software developer, it is critical that the programs we produce are simple to use and comprehend. One useful method is to present data in a format that is basic and understandable to the ordinary user. Fortunately, the Humanizer package greatly simplifies the work of presenting data in a natural and user-friendly manner for.NET developers. In this post, we will look at how developers can use the Humanizer package to prepare data in a human-readable fashion. We’ll also give you some C# examples to help you understand how to utilize the library successfully.

What exactly is Humanizer?

Humanizer is a free, open-source.NET toolkit that provides a collection of extension methods and utilities for formatting and manipulating strings, numbers, dates, timings, and other data in a way that people can read and understand. It aims to make your code more user-friendly by removing the need for complicated formatting logic. Humanizer allows you to easily convert numbers into words, format date and time intervals, pluralize words, and conduct a variety of other tasks.

Getting Started with Humanizer

Let’s dive right in and see how to get started with Humanizer in your .NET (C#) project.

1. Install the Humanizer NuGet Package

The first step is to install the Humanizer NuGet package in your project. Here’s how you can do it:

  1. Open your Visual Studio project or create a new one.
  2. Right-click on your project in the Solution Explorer, and select “Manage NuGet Packages.”
  3. In the “Browse” tab, search for “Humanizer” and select it from the list of packages.
  4. Click the “Install” button to add Humanizer to your project.

2. Import the Humanizer Namespace

In your C# code, import the Humanizer namespace at the top of your file to access Humanizer’s extension methods:

using Humanizer;

Using Humanizer in Your .NET (C#) Application

Now that you have Humanizer installed and the namespace imported, you can start using its features. Here are some common use cases and examples:

String Formatting

string camelCaseString = "thisIsCamelCase";
string humanizedString = camelCaseString.Humanize(); // "This Is Camel Case"

Pluralization and Singularization

string singular = "apple".ToSingular();   // "apple"
string plural = "apple".ToPlural();       // "apples"

Formatting Numbers with Units

int num = 5;
string formatted = num.ToWords(); // "five"

Date and Time Formatting

DateTime dateTime = DateTime.Now;
string formattedDate = dateTime.Humanize(); // "a moment ago" or "2 hours ago"

Ordinal Numbers

int number = 1;
string ordinal = number.ToOrdinalWords(); // "first"

The Humanizer library can help you achieve a lot of great things. It simplifies the process of making your application’s output more user-friendly and easy to read. This is especially useful when working with user interfaces, generating human-readable logs or formatting data for reports. So, if you want to make your app more intuitive and user-friendly, you should definitely give it a try.

Humanizer is a valuable tool for any .NET developer looking to format data in a natural way using C#. With its wide range of features, Humanizer simplifies the process of presenting data in a more user-friendly format, making your applications more approachable. Whether you need to format strings, pluralize words, or make numbers and dates more understandable, Humanizer has got you covered. By incorporating Humanizer into your next project, you can save time and ensure a better user experience for your audience.

ASP.NET Core 8 Hosting Recommendation is a popular recommendation that offers various hosting choices. Starting from shared hosting to dedicated servers, you will find options fit for beginners and popular websites. It offers various hosting choices if you want to scale up. Also, you get flexible billing plans where you can choose to purchase a subscription even for one or six months.