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;

$money = Money::EUR('100'); // 1€

$increasedMoney = Operation::of($money)->percentageIncrease('20') // 1.20€

use Money\Money; 
use MoneyOperation\Operation;

$money = Money::EUR('288'); // 2.88€

// percentageDecrease accepts positive and negative numeric strings
$decreasedMoney = Operation::of($money)->percentageDecrease('2.99') // 2.79€
$decreasedMoney = Operation::of($money)->percentageDecrease('-2.99') // 2.79€

use Money\Money; 
use MoneyOperation\Operation;

$moneyA = Money::EUR('100'); // 1€
$moneyB = Money::EUR('120'); // 1.20€

// Returns a float. Use number_format to format the result 
$percentage = Operation::of($moneyA)->percentageDifference($moneyB) // 20.0

use Money\Money; 
use MoneyOperation\Operation;

$money = Money::EUR('1000'); // 10€

/**
 * Will try to increase the first part when cannot be split equally
 * Throws \MoneyOperation\Exceptions\InvalidOperationException when cannot be split at all (for very low values mainly)
 */
$parts = Operation::of($money)->split(3) // [Money::EUR('334'), Money::EUR('333'), Money::EUR('333')]

use Money\Money; 
use MoneyOperation\Operation;

$parts = [Money::EUR('334'), Money::EUR('333'), Money::EUR('333')];

$money = Operation::join($parts) // 10€

use Money\Money; 
use MoneyOperation\Operation;

$parts = [Money::EUR('100'), Money::EUR('200'), Money::EUR('300'), Money::EUR('400')];

$money = Operation::average($parts) // 2,50€

use Money\Money; 
use MoneyOperation\Operation; 

/** 
 * Uses \Money\Formatter\IntlMoneyFormatter
 * Throws \MoneyOperation\Exceptions\InvalidOperationException when intl extension is not available
 */
$money = Operation::of(Money::USD('100'))->format('en_US') // $1.00 

use Money\Money; 
use MoneyOperation\Operation; 

/** 
 * Uses \Money\Parser\IntlMoneyParser
 * Throws \MoneyOperation\Exceptions\InvalidOperationException when intl extension is not available
 */
$money = Operation::parse('$1.00', 'en_US') // Money::USD('100') 

use Money\Money; 
use MoneyOperation\Operation; 

/** 
 * Uses \Money\Parser\DecimalMoneyFormatter
 */
$money = Operation::of(Money::EUR(54321))->toDecimal() // double(543.21) 

use Money\Money; 
use MoneyOperation\Operation; 

/**
 * @param int|numeric-string $amount
 * @param Currency|non-empty-string $currency
 */
$money = Operation::factory(100, 'EUR') // Money::EUR('100')