PHP code example of ez-php / money

1. Go to this page and download the library: Download ez-php/money 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/ */

    

ez-php / money example snippets


use EzPhp\Money\Money;

$price  = Money::of('19.99', 'EUR');          // from decimal string
$tax    = Money::ofMinorUnits(199, 'EUR');     // from cents → 1.99 EUR
$zero   = Money::zero('USD');

// JPY has no minor units (scale = 0)
$yen    = Money::of('1000', 'JPY');

use EzPhp\BigNum\RoundingMode;

$total  = $price->add($tax);                  // 21.98 EUR
$half   = $price->divide(2, RoundingMode::HALF_UP);
$vat    = $price->multiply('0.19');           // 3.80 EUR (rounded HALF_UP)

// Split 10.00 EUR three ways — no cent is lost
[$a, $b, $c] = Money::of('10.00', 'EUR')->allocate([1, 1, 1]);
// 3.34 EUR, 3.33 EUR, 3.33 EUR  (total = 10.00 EUR ✓)

// Weighted split
[$deposit, $remainder] = Money::of('100.00', 'EUR')->allocate([1, 3]);
// 25.00 EUR, 75.00 EUR

$price->isGreaterThan($tax);   // true
$price->isEqualTo($tax);       // false
$price->compareTo($tax);       // 1

$price->toMinorUnits()->toString();  // "1999"

use EzPhp\Money\Currency;

$eur = Currency::of('EUR');
$eur->getCode();        // "EUR"
$eur->getNumericCode(); // "978"
$eur->getName();        // "Euro"
$eur->getScale();       // 2
$eur->getSymbol();      // "€"

use EzPhp\Money\Formatter\DecimalMoneyFormatter;
use EzPhp\Money\Formatter\IntlMoneyFormatter;

$plain = new DecimalMoneyFormatter();
$plain->format($price);                        // "19.99"

$withCode = new DecimalMoneyFormatter(includeCurrencyCode: true);
$withCode->format($price);                     // "19.99 EUR"

$intl = new IntlMoneyFormatter('de_DE');        // 

use EzPhp\Money\Parser\DecimalMoneyParser;
use EzPhp\Money\Parser\IntlMoneyParser;

$parser = new DecimalMoneyParser();
$money  = $parser->parse('19.99', 'EUR');

$intlParser = new IntlMoneyParser('en_US');     // 

use EzPhp\Money\CurrencyRegistry;

CurrencyRegistry::has('EUR');    // true
CurrencyRegistry::has('ZZZ');   // false
CurrencyRegistry::all();         // array<string, Currency>
bash
composer