keiosweb / moneyright

Arbitrary precision arithmetic-based Money value object following GAAP rounding principles
MIT License
20 stars 4 forks source link

Keios/MoneyRight

Latest Version Software License Build Status Coverage Status Quality Score Total Downloads

Arbitrary precision arithmetic-based Money value object. Drop-in replacement for Mathias Verraes' Money library (some use statement tweaking required). Follows GAAP suggestion to use 4 decimal places with rounding on 5th to minimize statistical influence of rounding errors.

Follows PSR-2 guidelines.

Project name is inspired by Kanye West's verse:

La la la la... wait till I get my money right!

As we all know money really should be right (and therefore not stored in floats!).

Requirements

PHP 5.4.0+ BCMath Arbitrary Precision Arithmetic PHP extension

Install

Via Composer

$ composer require keios/moneyright

Usage

$tenEuroNetPrice = Keios\MoneyRight\Money::EUR('10'); // Money::EUR(10000) integers as cents | Money::EUR(10.0) floats as literal amount

var_dump($tenEuroNetPrice->getAmount()); // int(1000) - cents
var_dump($tenEuroNetPrice->getAmountString()); // string(7) "10.0000" - literal amount in string with 4 decimal points precision

$vatTax = $tenEuroNetPrice->multiply('0.23'); // 23% VAT tax

var_dump(assert(!$vatTax->equals($tenEuroNetPrice))); // bool(true)
var_dump($vatTax->getAmountString()); // string(6) "2.3000"

$grossPrice = $tenEuroNetPrice->add($vatTax); // instances are immutable, so every operation returns new instance

var_dump($grossPrice->getAmountString()); // string(7) "12.3000"
var_dump($grossPrice->getAmount()); // int(1230) - cents

Testing

$ phpunit

Contributing

Please see CONTRIBUTING for details.

Credits

License

The MIT License (MIT). Please see License File for more information.