2017 © Pedro Peláez
 

library moneyobject

A PHP library providing immutable Money value object

image

fortis/moneyobject

A PHP library providing immutable Money value object

  • Sunday, July 1, 2018
  • by fortis
  • Repository
  • 1 Watchers
  • 1 Stars
  • 346 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 8 Versions
  • 66 % Grown

The README.md

moneyobject

Travis Scrutinizer Code Quality Coveralls Packagist FOSSA Status, (*1)

A PHP library providing immutable Money value object with arbitrary-precision and solution for floating point rounding errors., (*2)

What do you think will be printed in the example below? ``` php print (36 - 35.99) === 0.01 ? '✅ equals' : 'not equals 😈';, (*3)

Actually `not equals 😈` . You can try [https://ideone.com/2UQlBF](https://ideone.com/2UQlBF). 

> Squeezing infinitely many real numbers into a finite number of bits requires an approximate representation. Although there are infinitely many integers, in most programs the result of integer computations can be stored in 32 bits. In contrast, given any fixed number of bits, most calculations with real numbers will produce quantities that cannot be exactly represented using that many bits. Therefore the result of a floating-point calculation must often be rounded in order to fit back into its finite representation. This rounding error is the characteristic feature of floating-point computation.
>
> *-- [Oracle](https://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html)*

## Install

Install directly from command line using Composer
``` bash
composer require fortis/moneyobject

QuickStart

Currency code validation

``` php $money = new Money(100, 'USF'); // throws InvalidCurrencyException, (*4)


#### Create Money instance ``` php $money = Money::USD(100.20); // 100.20 USD. Short syntax with autocomplete. $money = new Money(100.20, CurrencyCode::USD); // 100.20 USD $money = Money::create(100.20, CurrencyCode::USD); // 100.20 USD

Get currency

``` php $money->getCurrency()->getCode(); // USD, (*5)


#### Get amount ``` php $money->getAmount()->toFloat(); // 100.20

Multiply: 100.20 * 2

``` php $money->multiply(2) ->getAmount()->toFloat(); // 200.40, (*6)


#### Divide: 100.20 / 2 ``` php $money->divide(Money::USD(2)) ->getAmount()->toFloat(); // 50.10

Plus: 100.20 + 2.5

``` php $money->plus(Money::USD(2.5)) ->getAmount()->toFloat(); // 102.70, (*7)


#### Minus: 100.20 - 0.5 ``` php $money->minus(Money::USD(0.5)) ->getAmount()->toFloat(); // 99.70

Minus: 36 - 35.99

``` php Money::USD(36)->minus(Money::USD(35.99)) ->getAmount()->toFloat(); // 0.01, (*8)


#### Minus: 36 - 35.99 ``` php Money::USD(36)->minus(35.99) ->getAmount()->toFloat(); // 0.01

Convert USD to EUR

$ composer require florianv/swap php-http/message php-http/guzzle6-adapter

php $swap = (new SwapBuilder()) ->add('fixer') ->build(); $converter = new Converter($swap); $usd50 = Money::USD(50); $result = $converter->convert($usd50, Currency::EUR());, (*9)

Credits

License

moneyobject is licensed under the MIT license., (*10)

FOSSA Status, (*11)

The Versions

01/07 2018

dev-master

9999999-dev https://github.com/fortis/moneyobject

A PHP library providing immutable Money value object

  Sources   Download

MIT

The Requires

 

The Development Requires

by Alan Bondarchuk

currency money

12/06 2018

1.0.6

1.0.6.0 https://github.com/fortis/moneyobject

A PHP library providing immutable Money value object

  Sources   Download

MIT

The Requires

 

The Development Requires

by Alan Bondarchuk

currency money

29/05 2018

1.0.5

1.0.5.0 https://github.com/fortis/moneyobject

A PHP library providing immutable Money value object

  Sources   Download

MIT

The Requires

 

The Development Requires

by Alan Bondarchuk

currency money

07/11 2017

1.0.4

1.0.4.0 https://github.com/fortis/moneyobject

A PHP library providing immutable Money value object

  Sources   Download

MIT

The Requires

 

The Development Requires

by Alan Bondarchuk

currency money

27/09 2017

1.0.3

1.0.3.0 https://github.com/fortis/moneyobject

A PHP library providing immutable Money value object

  Sources   Download

MIT

The Requires

 

The Development Requires

by Alan Bondarchuk

currency money

11/09 2017

1.0.2

1.0.2.0 https://github.com/fortis/moneyobject

A PHP library providing immutable Money value object

  Sources   Download

MIT

The Requires

 

The Development Requires

by Alan Bondarchuk

currency money

11/09 2017

1.0.1

1.0.1.0 https://github.com/fortis/moneyobject

A PHP library providing immutable Money value object

  Sources   Download

MIT

The Requires

 

The Development Requires

by Alan Bondarchuk

currency money

11/09 2017

1.0.0

1.0.0.0 https://github.com/fortis/moneyobject

A PHP library providing immutable Money value object

  Sources   Download

MIT

The Requires

 

The Development Requires

by Alan Bondarchuk

currency money