Coder Social home page Coder Social logo

andremarcondesteixeira / teixeirasoftware.finance.money Goto Github PK

View Code? Open in Web Editor NEW
7.0 2.0 2.0 333 KB

A simple cross-platform money class library

Home Page: https://andremarcondesteixeira.github.io/TeixeiraSoftware.Finance.Money/

License: MIT License

C# 100.00%
csharp dotnet-core finance currency money class-library accounting cross-platform-money currencies nuget

teixeirasoftware.finance.money's Introduction

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.

teixeirasoftware.finance.money's People

Contributors

andremarcondesteixeira avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

teixeirasoftware.finance.money's Issues

Refactor 8 lines occurring 2 times in MoneyMathOperators.cs

I've selected for refactoring 8 lines of code which are duplicated in 1 file(s) (1, 2). Addressing this will make our codebase more maintainable and improve Better Code Hub's Write Code Once guideline rating! ๐Ÿ‘

Here's the gist of this guideline:

  • Definition ๐Ÿ“–
    Do not copy code.
  • Whyโ“
    When code is copied, bugs need to be fixed in multiple places. This is both inefficient and a source of regression bugs.
  • How ๐Ÿ”ง
    Avoid duplication by never copy/pasting blocks of code and reduce duplication by extracting shared code, either to a new unit or introduce a superclass if the language permits.

You can find more info about this guideline in Building Maintainable Software. ๐Ÿ“–


โ„น๏ธ To know how many other refactoring candidates need addressing to get a guideline compliant, select some by clicking on the ๐Ÿ”ฒ next to them. The risk profile below the candidates signals (โœ…) when it's enough! ๐Ÿ


Good luck and happy coding! :shipit: โœจ ๐Ÿ’ฏ

Refactor 9 lines occurring 2 times in MoneyComparisonOperators.cs

I've selected for refactoring 9 lines of code which are duplicated in 1 file(s) (1, 2). Addressing this will make our codebase more maintainable and improve Better Code Hub's Write Code Once guideline rating! ๐Ÿ‘

Here's the gist of this guideline:

  • Definition ๐Ÿ“–
    Do not copy code.
  • Whyโ“
    When code is copied, bugs need to be fixed in multiple places. This is both inefficient and a source of regression bugs.
  • How ๐Ÿ”ง
    Avoid duplication by never copy/pasting blocks of code and reduce duplication by extracting shared code, either to a new unit or introduce a superclass if the language permits.

You can find more info about this guideline in Building Maintainable Software. ๐Ÿ“–


โ„น๏ธ To know how many other refactoring candidates need addressing to get a guideline compliant, select some by clicking on the ๐Ÿ”ฒ next to them. The risk profile below the candidates signals (โœ…) when it's enough! ๐Ÿ


Good luck and happy coding! :shipit: โœจ ๐Ÿ’ฏ

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.