PHP code example of rogervila / moneyphp-operations

1. Go to this page and download the library: Download rogervila/moneyphp-operations library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

rogervila / moneyphp-operations example snippets


use Money\Money;
use MoneyOperation\Operation;

// From a Money object
$operation = Operation::of(Money::EUR(1000));

// From values (amount and currency)
$operation = Operation::ofValues(1000, 'EUR');

$money = Money::EUR('100'); // 1.00€
$increased = Operation::of($money)->percentageIncrease('20'); // 1.20€

// Custom rounding mode
$increased = Operation::of($money)->percentageIncrease('20', Money::ROUND_HALF_DOWN);

$money = Money::EUR('288'); // 2.88€
$decreased = Operation::of($money)->percentageDecrease('2.99'); // 2.79€

$moneyA = Money::EUR('100');
$moneyB = Money::EUR('120');

$diff = Operation::of($moneyA)->percentageDifference($moneyB); // 20.0

$money = Money::EUR('1000'); // 10.00€
$parts = Operation::of($money)->split(3); 
// [Money::EUR('334'), Money::EUR('333'), Money::EUR('333')]

$parts = [Money::EUR('334'), Money::EUR('333'), Money::EUR('333')];
$sum = Operation::join($parts); // 10.00€

$parts = [Money::EUR('334'), Money::EUR('333'), Money::EUR('333')];
$isValid = Operation::of(Money::EUR(1000))->assertSplit($parts); // true

$parts = [Money::EUR('100'), Money::EUR('200'), Money::EUR('300'), Money::EUR('400')];
$avg = Operation::average($parts); // 2.50€

$money = Money::USD('100');
$formatted = Operation::of($money)->format('en_US'); // "$1.00"

// Custom currencies implementation
$formatted = Operation::of($money)->format('en_US', new MyCustomCurrencies());

$money = Operation::parse('$1.00', 'en_US'); // Money::USD('100')

$decimal = Operation::of(Money::EUR(54321))->toDecimal(); // 543.21

$money = Operation::factory(100, 'EUR'); // Money::EUR('100')
$money = Operation::factory('500', new \Money\Currency('USD')); // Money::USD('500')