Coder Social home page Coder Social logo

money's Introduction

Money

A basic example implementation of the Fowler's Money pattern. The library performs money operations using the currency's smallest unit to prevent rounding errors.

<?php

use Money;

$euro = Money::eur(100);
$tenEuro = $euro + Money::eur(900);

print($tenEuro->format()); // 10,00 €

$shares = $tenEuro->allocate([1,1,1]);

print($shares[0]->format()); //3,34 €
print($shares[1]->format()); //3,33 €
print($shares[2]->format()); //3,33 €

Usage

Instantiation

Money amount is represented in currency's smallest units / cents. (e.g. 100 for 1 euro).

$euro = Money::eur(100);

print($euro->getAmount()); // 100

Operations

  • add

  • subtract

  • multiply

  • divide

Add

Sum amount of two money objects using the add method. Addition must be made between objects with the same currency.

$fiveEuro = Money::eur(500);

$tenEuro = $fiveEuro->add($fiveEuro);

Subtract

Subtract amount of two money objects using the subtract. Subtraction must be made between objects with the same currency.

$fiveEuro = Money::eur(500);

$zeroEuro = $fiveEuro->subtract($fiveEuro);

Multiply

Multiply amount using the multiply method.

$fiveEuro = Money::eur(500);

$tenEuro = $fiveEuro->multiply(2);

Divide

Divide amount using the divide method.

$tenEuro = Money::eur(500);

$fiveEuro = $tenEuro->divide(2);

Allocation

  • Allocate

Allocate

Split money amount according to provided ratios. Remaining amount is distributed to shares with the biggest ratios.

$tenEuro = Money::eur(1000);

$shares = $tenEuro->allocate([1,1,1]);

print($shares[0]->getAmount()); //334
print($shares[1]->getAmount()); //333
print($shares[2]->getAmount()); //333

Comparison

Equals

Compare two money objects using the equals method. The method will return false when amount or currency type is the same.

$tenEuro = Money::eur(1000);
$oneEuro = Money::eur(100);
$oneDollar = Money::usd(100);

$oneEuro->equals($tenEuro); // false
$oneEuro->equals($oneDollar); // false

GreaterThan

Check if money amount is larger than the given money amount using the greaterThan method.

$tenEuro = Money::eur(1000);
$oneEuro = Money::eur(100);

$tenEuro->greaterThan($oneEuro); // true

GreaterThanOrEqual

Check if money amount is larger or equal to the given money amount using the greaterThanOrEqual method.

$oneEuro = Money::eur(100);

$oneEuro->greaterThanOrEqual($oneEuro); // true

LessThan

Check if money amount is less than the given money amount using the lessThan method.

$tenEuro = Money::eur(1000);
$oneEuro = Money::eur(100);

$oneEuro->lessThan($tenEuro); // true

LessThanOrEqual

Check if money amount is lessor equal to the given money amount using the lessThanOrEqual method.

$oneEuro = Money::eur(100);

$oneEuro->lessThanOrEqual($oneEuro); // true

Format

Format the money using the format method.

$oneEuro = Money::eur(100);

print($oneEuro->format()); // 1,00 €

Testing

phpunit

License

The MIT License (MIT). See the license file for more information.

money's People

Contributors

luka-mladenovic avatar

Watchers

 avatar  avatar

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.