PHP code example of doxadoxa / money-lib

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

    

doxadoxa / money-lib example snippets


use Money\Currency;
use Money\Money;

$bitcoin = new Currency('BTC', 8);
$amount = Money::make( $bitcoin, 0.1858);
echo $amount->getAmount(); // (float) 0.1858
echo $amount->getStringAmount(); // (string) "18580000"

use Money\Currency;
use Money\Money;

$ethereum = new Currency('ETH', 18);
$amount = Money::make( $ethereum, 0.000000000000000005);
echo $amount->getAmount(); // (float) 5.0E-18
echo $amount->getStringAmount(); // (string) "5"

use Money\Currency;
use Money\Money;

$ethereum = new Currency('ETH', 18);
$usd = new Currency('USD', 2);
$amount = Money::make( $ethereum, 0.05 );

// Add operation
$newAmount = $amount->add( Money::make( $ethereum, 0.005 ) );
echo $newAmount->getAmount(); // (float) 0.055

// Sub operation
$newAmount = $amount->sub( Money::make( $ethereum, 0.005 ) );
echo $newAmount->getAmount(); // (float) 0.045

// Comparisons
$amount = Money::make( $usd, 10 );
$newAmount = Money::make( $usd, 10);
$nullAmount = Money::make( $ethereum, 0);
$newNullAmount = Money::make( $usd, 0);

$amount->equals( $newAmount ); // (bool) true
$nullAmount->equals($newNullAmount); // (bool) true

$amount->strictEquals( $newAmount ); // (bool) true
$nullAmount->strictEquals($newNullAmount); // (bool) false

$newAmount = Money::make( $usd, 20 );
$amount->less( $newAmount ); // (bool) true
$amount->more( $newAmount ); // (bool) false


use Money\Currency;
use Money\Money;

$usd = new Currency('USD');
$money = Money::make( $usd, 1000 );
echo $money->format();// $ 1,000.00

use Money\Formatters\CurrencyFormatter;
use Money\Currency;
use Money\Money;

$usd = new Currency('USD');
$formatter = new CurrencyFormatter(":amount:symbol", '.', '', 0);
$usd->setFormatter( $formatter );
$money = Money::make( $usd, 1000);
echo $money->format(); // 1000$