NMoneys for Developers

 
event

So you heard about NMoneys. You looked and the information and it caught on you. It happens that you are a .NET developer indeed. You are welcome to go on reading, but it is really not for you. You already know it.
So you have not heard of NMoneys. It does not ring any bell. It happens that you are a .NET developer indeed. You are welcome to go on reading, this is for you. It might help you answering some of all of these questions: why would you want to use it? I’d like to use, but, how?

So you have not heard of NMoneys. It does not ring any bell. It happens that you are not a .NET developer indeed. You are welcome to go on reading, but… You might enjoy the style of writing, but… We still like you, nobody’s perfect after all ;-p Please do pass the link to a developer you may know. It does not have to know .NET though. Even them have brilliant ideas ;-p

Installation

  • v1Download the binaries. I will be covering version 1 features.
  • Unpack the file to your favourite project dependencies folder
  • Add a reference from your project to NMoneys.dll (have System.Xml referenced if it is not already referenced. Fear not, no angly brackets tax to be paid)
  • Import the NMoneys namespace
  • Start writing NMoneys-enabled-awesome-codeTM

Is that it? That simple? Yes, that simple.

Working with NMoneys.Currency

First thing one can do is getting a Currency instance. Use of the “static shortcuts” provided:

What if your currency is different from the ones in the shortcuts? Or maybe you already have some sort of currency identifier. In those cases there are other creation methods, for example, one of the Currency.Get() overloads:

If catching exceptions is not your favourite pastime when data is not 100% correct try using a Currency.TryGet() overload:

One might wonder what happened to good-old constructors. Answer is that currencies are a finite set of immutable objects, being of little use the existence of different instances representing the same idea. The singleton-like behaviour was chosen over other alternatives, that is why you can’t “new-up” Currency instances. Just so you know, Currency is a reference type.

Working with NMoneys.Moneys

Having currencies and nothing else is like… well, having currencies and no monetary quantities. Let’s create those monetary quantities:

Besides, there are some more meaningfully named creation methods:

There is a quicker, more succinct  way to instantiate monetary quantities: using extension methods. Import the additional NMoneys.Extensions namespace and use those shortcuts. Ideal for unit testing scenarios:

Money instances can be compared and implemented comparison operands. Mind you, they have to have the same currency to be compared:

Value equality can be checked, as IEquatable<Money> is implemented:

Simple arithmetic can be performed:

More unary operations that involve the quantity:

And if the operation that you want to perform is not available, extensibility points are provided:

And last, but not least, displaying monetary quantities. This is probably, the number one Raison d'être of this library so you should be eager to see what it can do:

If your country happens to share currency with other countries, but the format is not quite the same, there is a Money.ToString() overload that might come handy:

For more hard-core formatting, use Money.Format():

And remember, Money (as opposed to Currency) is a Value Type. Amongst other things, it means it is cheap to create and small in memory footprint and it is also immutable.

What now?

If you read this far, congratulations, you are a real champ.

Secondly, you might have figured out whether this library is of any use to you. (I hope it is a yes)
Regardless the answer there are a couple of things I want to ask you:

Jolly coding