How many times have you had to ask a colleague what unit that external application is using? Or worse, had bugs because you forgot to divide by 100?
In many applications, it is common to change values when they are received by dividing or multiplying them. This is done because the other application uses centimeters, while our application uses meters, or some similar difference.
There are libraries that mitigate this, but I didn’t find one to my liking. I’ve used similar libraries in the past, but these stayed inside the companies I was working at.
So I’ve created RedStar.Amounts (RedStar being my company name).
You can install it easily via Nuget and it allows you to write readable code like this:
var length = new Amount(4, LengthUnits.Meter); var width = new Amount(3, LengthUnits.Meter); var surface = length * width; Console.WriteLine(surface); // 12 m²
Its built on existing code by Rudi Breedenraedt, but with optimizations and enhancements (like some helper methods and support for JSON.NET). And putting it on GitHub allows me to accept pull requests and fix issues anyone else has.
For reference, I’ve been using this code in an industrial setting, in production, without problems.
I’ve also added documentation on the wiki.
So if you can use this, or are just interested, please check it out. I’m confident this can help you write more legible and less error-prone code.