Almost a year ago now, I introduced RedStar.Amounts, a .NET library to handle units and amounts in an easy way.
The code
It allows you to explicitly state what unit a certain amount is measured in. So instead of doing this:
var lengthInCm = 400;
You can do this:
var length = new Amount(400, LengthUnits.CentiMeter);
What’s more, you can perform calculations without having to wonder what the unit is:
var length = new Amount(1000, LengthUnits.CentiMeter); var width = new Amount(1, LengtUnits.Meter); var area = length * width; // will be 10m²
This makes working with units and amounts very easy and expressive, and avoids hard-to-find errors because units were passed in some wrong unit.
There’s more to it than that of course. You can:
- easily create composite units
- easily create new units
- perform math
- use it with JSON.NET
I’ve done my best to document it all, but if you find anything missing, don’t hesitate to create a new issue. Please also create an issue if you have an idea for an improvement or find a bug, of course.
Is it production-ready?
Yes, it is currently used in an appliction at a large steel manufacturing company, without any issues.
Where can I get it?
On NuGet of course! RedStar.Amounts consists of three Nuget packages:
- RedStar.Amounts: the core library
- RedStar.Amounts.StandardUnits: most likely what you need, will include standard units (length, weight, energy, etc)
- RedStar.Amounts.JsonNet: includes custom converters for JSON.NET
.NET Standard
I recently ported it to .NET Standard 2.0. The 2.x releases will be targeting .NET Standard 2.0 going forward. So you should be able to use it on .NET Core, .NET Framework 4.6.1 and up, Mono, Xamarin, UWP,… Anything that supports .NET Standard 2.0.
Credit where it’s due
It was based off code I had found on CodeProject, but needed to improve. I initially put this in a library for the specific client I was working for (a large, industrial steel manufacturer), but realized this could be used by other organizations too. I realized this because it was the second client I had encountered such a library.
What’s with the name?
Yes, the name… Red Star IT is the name of my company (I’m an independant consultant), so it’s a bit like Newtonsoft.JSON. Except that nobody calls it that, the real name is JSON.Net. So if anyone can help me with a better name (Amounts.Net?), please feel free to contact me (@petermorlion). I might just send you a great Belgian beer 😉