The Humanizer

I don’t really have much to say about the Humanizer library that’s not already written on their Github readme.

This post is more of a reminder post, just to remind me about the library because the name of it eluded me the other day when I wanted to use it.

So what is Humanizer?

If you’ve got strings, enums, dates, times (and more) that you need to convert to a more human readable string, then Humanizer can help. Equally if you have a string that you want to make less human readable you can use Humanizer (bare with me, the less human readable might be of use).

An example

I’ll use the real world scenario I needed to use Humanizer on.

I have data coming from a web service which returns field/property names and values. The names are in standard camel case, i.e. eligibilityFlag. Now this is fine, but I wanted the end user to see these names in a more human readable way, i.e. “eligibilityFlag” should display as “Eligibility flag”. It’s just nicer, looks better and is more readable for the end user.

Ofcourse, it’s not too difficult to write the code to do this yourself, but why bother when you can fire up Nuget or the Package Manager Console and locate the Humanizer package or run Install-Package Humanizer.

Just using the following

// don't forget using Humanizer

Console.WriteLine("eligibilityFlag".Humanize());

and the output will be Eligibility flag.

We can easily convert this back to the camel case computer style text using

Console.WriteLine("Eligibility flag".Dehumanize().Camelize());

Humanizer can also help with numbers to strings, for example convert numbers to works or to an easier to read format. If we have the value 123456 we might like to output and 123.456K or maybe as one hundred and twenty-three thousand four hundred and fifty-six using

Console.WriteLine(123456.ToWords());
Console.WriteLine(123456.ToMetric());

we can convert the metric representation back using

Console.WriteLine("123.456k".FromMetric());