Value object for handling monetary values
Via Composer:
composer require wsw/money
Instantiating a value:
<?php
use WSW\Money\Currency;
use WSW\Money\Money;
$money = new Money('1500.9999', new Currency('USD'));
Output values:
<?php
use WSW\Money\Currency;
use WSW\Money\Money;
$money = new Money("1500.9999", new Currency("USD"));
echo $money->getAmount(); // (string) 1500.999900
echo $money->getMicros(); // (integer) 1500999900
echo $money->getTruncate(); // (string) 1500.99
echo $money->getRound(); // (string) 1501.00
echo $money->getFormat(); // (string) 1.501,00
echo $money; // (string) 1.501,00
We verify that the values are equal:
<?php
use WSW\Money\Currency;
use WSW\Money\Money;
$money = new Money("1500.00", new Currency("USD"));
$money2 = new Money("1500.00", new Currency("USD"));
$money3 = new Money("1000.00", new Currency("USD"));
var_dump($money->equals($money2)); // bool(true)
var_dump($money->equals($money3)); // bool(false)
Compare values:
<?php
use WSW\Money\Currency;
use WSW\Money\Money;
$money = new Money("1500.00", new Currency("USD"));
$money2 = new Money("1500.00", new Currency("USD"));
$money3 = new Money("1000.00", new Currency("USD"));
// Returns zero (0) when values and currencies are equal.
var_dump($money->compare($money2)); // int(0)
// Returns one (1) when the left object has the value greater than the right object.
var_dump($money->compare($money3)); // int(1)
// Returns minus one (-1) when the right object has the value greater than the left object.
var_dump($money3->compare($money)); // int(-1)
Add value:
<?php
use WSW\Money\Currency;
use WSW\Money\Money;
$money = new Money('100', new Currency('USD'));
$addValue = new Money('50', new Currency('USD'));
$newMoney = $money->add($addValue);
echo $newMoney->getAmount(); // (string) 150.000000
echo $newMoney->getTruncate(); // (string) 150.00
echo $newMoney->getRound(); // (string) 150.00
echo $newMoney->getFormat(); // (string) 150,00
echo $newMoney; // (string) 150,00
Add percentage value:
<?php
use WSW\Money\Currency;
use WSW\Money\Money;
use WSW\Money\Percentage;
$money = new Money('100', new Currency('USD'));
$percent = new Percentage("75%");
$newMoney = $money->addPercent($percent);
echo $newMoney->getAmount(); // (string) 175.000000
echo $newMoney->getTruncate(); // (string) 175.00
echo $newMoney->getRound(); // (string) 175.00
echo $newMoney->getFormat(); // (string) 175,00
echo $newMoney; // (string) 175,00
Subtract value:
<?php
use WSW\Money\Currency;
use WSW\Money\Money;
$money = new Money('100', new Currency('USD'));
$subValue = new Money('60.75', new Currency('USD'));
$newMoney = $money->sub($subValue);
echo $newMoney->getAmount(); // (string) 39.250000
echo $newMoney->getTruncate(); // (string) 39.25
echo $newMoney->getRound(); // (string) 39.25
echo $newMoney->getFormat(); // (string) 39,25
echo $newMoney; // (string) 39,25
Subtract percentage value:
<?php
use WSW\Money\Currency;
use WSW\Money\Money;
use WSW\Money\Percentage;
$money = new Money('100', new Currency('USD'));
$percent = new Percentage("75%");
$newMoney = $money->subPercent($percent);
echo $newMoney->getAmount(); // (string) 25.000000
echo $newMoney->getTruncate(); // (string) 25.00
echo $newMoney->getRound(); // (string) 25.00
echo $newMoney->getFormat(); // (string) 25,00
echo $newMoney; // (string) 25,00
$ composer test
If you discover any security related issues, please email [email protected] instead of using the issue tracker.
The MIT License (MIT). Please see License File for more information.