TeixeiraSoftware.Finance.Money 5.0.2

dotnet add package TeixeiraSoftware.Finance.Money --version 5.0.2                
NuGet\Install-Package TeixeiraSoftware.Finance.Money -Version 5.0.2                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="TeixeiraSoftware.Finance.Money" Version="5.0.2" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add TeixeiraSoftware.Finance.Money --version 5.0.2                
#r "nuget: TeixeiraSoftware.Finance.Money, 5.0.2"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install TeixeiraSoftware.Finance.Money as a Cake Addin
#addin nuget:?package=TeixeiraSoftware.Finance.Money&version=5.0.2

// Install TeixeiraSoftware.Finance.Money as a Cake Tool
#tool nuget:?package=TeixeiraSoftware.Finance.Money&version=5.0.2                

TeixeiraSoftware.Finance.Money

TeixeiraSoftware.Finance.Money

Build status license Pull Requests NuGet

A simple cross platform money struct for .Net based on Martin Fowler's Money pattern.

This library is compatible with .Net Standard 2.0 (see https://docs.microsoft.com/en-us/dotnet/standard/net-standard for details).

TeixeiraSoftware.Finance.Money is an immutable value type.

Installation

NuGet Package Manager:

Install-Package TeixeiraSoftware.Finance.Money

.NET CLI:

dotnet add package TeixeiraSoftware.Finance.Money

Paket CLI:

paket add TeixeiraSoftware.Finance.Money

Dependencies

This package has no dependencies.

Usage

The Money class makes use of an abstract class called ICurrency that must be implemented in order to use this library. There is already an implementation available at nuget using the name TeixeiraSoftware.Finance.Currency, and for the examples in this documentation, the methods used to get ICurrency instances will come from that library. If you want, you can implement yourself the ICurrency abstract class in order to provide your own functionallity (The ICurrency abstract class is part of TeixeiraSoftware.Finance.Money and you can optionally use the implementation provided by TeixeiraSoftware.Finance.Currency).

The usage is very simple:

// XXX represents the ISO 4217 code of the currency
var tenMoneys = new Money(12.3m, Currency.XXX);
var aMillionMoneys = new Money(1000000, Currency.ByAlphabeticCode("XXX"));

Math operations:

+ operator (addition):

When doing addition operations, you can sum an instance of Money directly to a decimal or an integer. The resulting Money instance will have the same currency as the instance used in the addition operation.

var halfMoney = new Money(0.25m, Currency.XXX) + 0.25m;
var tenMoneys = 1.5m + new Money(8.5m, Currency.XXX);
var aHundredMoneys = tenMoneys + 90;
var aThousandMoneys = 900 + aHundredMoneys;

And, of course, you can sum two instances of Money as well:

var moreMoney = aHundredMoneys + aThousandMoneys;
- operator (subtraction):

When doing subtraction operations, you can subtract an instance of Money directly to a decimal or an integer. The resulting Money instance will have the same currency as the instance used in the subtraction operation.

var oneMoney = new Money(2, Currency.XXX) - 1;
var tenMoneys = 1 - new Money(11, Currency.XXX);
var nineMoneys = tenMoneys - 1;
var eightMoneys = 1 - tenMoneys;

And, of course, you can subtract two instances of Money as well:

var sevenMoneys = eightMoneys - oneMoney;
/ operator (division):

You can only divide a Money instace by a factor, that can be a decimal or an integer:

var fiveHundredMoneys = aThousandMoneys / 2;
var someMoney = new Money(1000.01m, Currency.XXX) / 1.28m;

You cannot divide two Money instances, like:

// This will result in a compile time error
new Money(10, Currency.XXX) / new Money(10, Currency.XXX);

You also cannot divide any number to a Money instance, like:

// This will result in a compile time error
var money = 2 / new Money(10, Currency.XXX);

This is because those kind of operations do not make much sense.

* operator (multiplication):

You can only multiply a Money instance by a factor, that can be a decimal or an integer:

var someMoney = aThousandMoneys * 2.5m;
var aLotOfMoney = 1000 * aThousandMoneys;

You cannot multiply two Money instances, because it would not make much sense:

// This will result in a compile time error
var money = new Money(2, Currency.XXX) * new Money(10, Currency.XXX);

Comparison operations

The available operators are: ==, !=, >, >=, < and <=.

The .Equals method is also available.

var aThousandMoneys = new Money(1000, Currency.XXX);
var fortyTwoMoneys = new Money(42, Currency.XXX);
var twoThousandMoneys = aThousandMoneys * 2;
var aLotOfMoneys = 1000 * aThousandMoneys;

if (aThousandMoneys == aLotOfMoneys)
{
    // ...
}

if (aThousandMoneys != new Money(10, Currency.XTS))
{
    // ...
}

if (aThousandMoneys.Equals(twoThousandMoneys))
{
    // ...
}

if (twoThousandMoneys > aLotOfMoneys)
{
    // ...
}

// and so on...

Details about operations involving different currencies

You may pay attention when trying to use the operators when the currencies don't match. the operators >, >=, < and <= will throw a CurrencyMismatchException if the currencies are not the same:

var tenXXX = new Money(10, Currency.XXX);
var tenXTS = new Money(10, Currency.XTS);

if (tenXXX > tenXTS) // throws CurrencyMismatchException

However, you can perform an equality comparison between instances with different currencies, although in this case you will always obtain false:

var tenXXX = new Money(10, Currency.XXX);
var tenXTS = new Money(10, Currency.XTS);

if (tenXXX == tenXTS)
{
    // no exception bill be thrown and the code in this block will never be executed
}

if (tenXXX.Equals(tenXTS))
{
    // no exception bill be thrown and the code in this block will never be executed
}

If you don't like this behavior, you can change it by setting the flag StrictEqualityComparisons to true. When doing so, any try to compare the equality between instances with different currencies will result in a CurrencyMismatchException being thrown:

Money.StrictEqualityComparisons = true;

var tenXXX = new Money(10, Currency.XXX);
var tenXTS = new Money(10, Currency.XTS);

if (tenXXX == tenXTS) // throws CurrencyMismatchException

if (tenXXX.Equals(tenXTS)) // throws CurrencyMismatchException

Contributing

You can see some topics that you can help with in the issues section of this project. You can also contribute by doing unit tests, writing documentation, making pull requests or sharing the project.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
5.0.2 16,966 12/11/2018
5.0.1 1,760 3/28/2018

This version turns ICurrency into a real interface.