Funcky.Money 1.3.0

dotnet add package Funcky.Money --version 1.3.0
NuGet\Install-Package Funcky.Money -Version 1.3.0
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="Funcky.Money" Version="1.3.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Funcky.Money --version 1.3.0
#r "nuget: Funcky.Money, 1.3.0"
#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 Funcky.Money as a Cake Addin
#addin nuget:?package=Funcky.Money&version=1.3.0

// Install Funcky.Money as a Cake Tool
#tool nuget:?package=Funcky.Money&version=1.3.0

funcky-money

Build Licence: MIT

Funcky.Money is an implementation of a versatile Money concept which addresses a lot of the problems you encounter when working with money quantities and currencies.

The implementation uses a lot of the concepts from the TDD implementation of Kent Beck in his book: Test-Driven Development by Example.

Package

NuGet package

How can Money be Funcky?

The Money approach is distinctly functional especially in comparison to the Money Pattern by Martin Fowler which just gives up when it comes to currencies.

The basic idea is, instead of trying to calculate the final amount in one step, we start with an Expression Tree (AST: Abstract Syntax Tree) of the operations we want to perform with Money objects.

This allows us to decouple the assembly and the evaluation of our calculations, our calculation are lazy and allow deferred execution.

Why not a money bag?

While the Money as an expression tree holds structural information which is ultimatly rarely necessary, the equality on a Money expression ultimatly needs a in almost all cases an exchange rate if more than one currency is involved. Therefore I do not accept this argument as an improvement. Especially if we support more than just sums (multiply, distribute) the money bag is very limited.

Quickstart

Construct a money object

var dollar = new Money(2.99m, Currency.USD);
var francs = Money.CHF(0.95m);

Create money expressions

var sum1 = dollar + francs;
var sum2 = francs.Add(Money.CHF(5));

var product1 = dollar * 3;
var product2 = francs.Multiply(1.5m);

// distribute the money into 3 equal parts
var distribution1 = product2.Distribute(3);

// distribute the money according to the given proportions in units of 0.05
var distribution2 = product2.Distribute(new []{7, 2, 1}, 0.05m);

Evaluate a MoneyExpression

Evaluate returns a money object according to the evaluation context.

var francs = francs.Add(Money.CHF(5)).Evaluate();

var context = MoneyEvaluationContext.Builder.Default
                .WithTargetCurrency(Currency.CHF)
                .WithExchangeRate(Currency.USD, 0.9004m)
                .WithRounding(RoundingStrategy.BankersRounding(0.05m))
                .Build();

var dollar = (dollar + francs).Evaluate(context);

For more examples, consult the testing project.

Requirements

These is the evolving list of TDD requirements which led to the implementation.

  • Use decimal Amount.
  • Support multiple currencies.
  • Add two Moneys in the same Currency (5USD + 9USD).
  • Cleanup Currency (XML handling should be extracted).
  • Add two Moneys with a different Currency (5USD + 10CHF).
  • Two moneys without Exchange rates should not Evaluate to a result.
  • Two moneys are equal if they have the same Currency and the same Amount.
  • Ability to round to 0.05 / distribute 1CHF as [0.35, 0.35, 0.30]
  • Evaluation passes through precision to result.
  • Override precision on evaluation.
  • Support different Exchange rates (evaluation).
  • Every construction of Money currently rounds to two digits, while this is interesting for 5.7f, it has bad effects in evaluation. We should remove the rounding again.
  • The default MidpointRounding mechanism is bankers rounding (MidpointRounding.ToEven).
  • Multiply a Money with a real number (int, and decimal).
  • Distribute Money equally into n slices (1CHF into 3 slices: [0.33, 0.33, 0.34]).
  • Distribute Money proportionally (1 CHF in 1:5 → [0.17, 0.83]).
  • Extract distribution of money into a strategy which is injected.
  • Support ISO 4217 Currencies.
  • Support calculations smaller than the minor unit? (on Money and EvaluationVisitor)
  • ToString supports correct cultural formatting and units.
  • Parse Money from string considering cultural formatting and units.
  • Support operators on the IMoneyExpression interface.
  • Convert currencies as late as possible (keep Moneybags per currency in the EvaluationVisitor).
  • Static constructor for most used Currencies, this could inject rules like precision: Money.CHF(2.00m)
  • To avoid rounding problems on construction, Money can only be constructed from decimal and int.
  • Add possibility to delegate the acquisition of exchange rates. (IBank interface)
  • There are a few throw Exception calls in the code which should be refined to specific exceptions.
  • There needs to be a NoRounding strategy, maybe provide an Interface IRoundingStrategy with a few given implementations.
  • Evaluation arithmetic Money operations can use different rounding mechanism.
  • Distribution with NoRounding strategy should distribute exactly according to the precision.
  • Distribution wich cannot exactly distribute money throws an ImpossibleDistributionException
  • Rounding only happens at the end of an evaluation.
  • Fix tests failing in other locales.
  • Write property tests using FsCheck.
  • Rounding is done at the end of every evaluation according to the rounding strategy.
  • The user can use arbitray rounding function, he just needs to implement the AbstractRoundingStratgey.
  • We do not round a Money on construction only on evaluation. You can create a 0.01m Money even if the precision is 0.1m.
  • Money distribution has a precision member, use that instead of the contrived Precision on Rounding.
  • Add unary and binary minus and the division operator.
  • The context has a smallest distribution unit.
  • A dimensionless factor can be calculated by dividing two money objects.

Decisions

  • We construct Money objects only from decimal and int. The decision how to handle external rounding problems should be done before construction of a Money object.
  • We keep Add, Multiply, etc because no all supported frameworks allow default implementations on the interface.
  • We prepare a distribution strategy but do not make it chosable at this point.
  • We support the following operators: unary + and -, and the binary operators ==, !=, +, -, * and /.
  • You can divide two different currencies only with the Divide(IMoneyExpression, IMoneyExpression, Option<MoneyEvaluationContext>) method where you have to give a MoneyEvaluationContext with the necessary exchange rates.
  • We removed the neutral Money element (Zero), there are too many unsolved problems. If we add it again we should probaly make a NeutralElement : IMoneyExpression.

Open Decisions

  • Implicit type conversion
    • Should A Money be constructible implicitly from a decimal?
    • Should adding a number to a money be possible (fiveDollars + 2.00m)?
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 is compatible.  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.
  • .NETStandard 2.0

    • Funcky (>= 3.0.0 && < 4.0.0)
  • net6.0

    • Funcky (>= 3.0.0 && < 4.0.0)

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
1.3.0 402 9/11/2023
1.2.0 142 8/21/2023
1.1.0 395 9/27/2022
1.0.1 398 1/7/2021
1.0.0 379 1/6/2021
1.0.0-beta.1 187 12/10/2020