superbalist / php-money
A money and currency library for handling arbitrary-precision arithmetic
Installs: 50 563
Dependents: 0
Suggesters: 0
Security: 0
Stars: 11
Watchers: 40
Forks: 1
Open Issues: 0
Requires
- php: >=5.4.0
- ext-bcmath: *
Requires (Dev)
- phpunit/phpunit: ~4.0
This package is auto-updated.
Last update: 2024-08-12 00:02:27 UTC
README
A money and currency library for handling arbitrary-precision arithmetic
Installation
composer require superbalist/php-money
Usage
use Superbalist\Money\CurrencyConversionServiceProvider; use Superbalist\Money\CurrencyFactory; use Superbalist\Money\Money; use Superbalist\Money\OpenExchangeRatesCurrencyConversionService; // set default currency conversion service // this is optional, and is only required if you're going to be converting between currencies $service = new OpenExchangeRatesCurrencyConversionService('[[insert app id here]]'); CurrencyConversionServiceProvider::setCurrencyConversionService($service); // set default currency CurrencyFactory::setDefault('ZAR'); // a money object can be constructed very loosely $a = new Money(150); $a = new Money('150'); // recommended $a = new Money('150.00'); $a = new Money(150.00); // not recommended, as we're trying to avoid using floating points $a = new Money('150', CurrencyFactory::make('USD')); // basic operations on numbers $a = new Money('150.55'); $b = new Money('99.45'); $c = $a->add($b); // 250 $a = new Money('300.00'); $b = new Money('2'); $c = $a->divide($b); // 150 // comparing numbers $a = new Money('0.33'); $b = new Money('0.33'); $c = $a->equals($b); // true $c = $a->isGreaterThan($b); // false $c = $a->isGreaterThanOrEqualTo($b); // true $c = $a->isLessThan($b); // false // convert a monetary value from currency A to currency B $a = new Money('100', CurrencyFactory::make('ZAR')); $b = $a->toCurrency(CurrencyFactory::make('USD')); // format a number for display $a = new Money('123.9999'); echo $a->format(); // 123.99 echo $a->display(); // R123.99 // handling value added tax $a = new Money('100'); $rate = '0.14'; $b = $a->calculateVat($rate); // 14 $c = $a->add($b); // 114 $d = $c->calculateNetVatAmount($rate); // 100 // min & max $a = new Money('100'); $b = new Money('101'); $c = $a->max($b); // 101 $d = $a->min($b); // 100 // ...see classes for full list of methods