PHP code example of tiny-blocks / money

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

    

tiny-blocks / money example snippets


use TinyBlocks\Math\BigNumber;
use TinyBlocks\Currency\Currency;

$currency = Currency::USD;
$bigNumber = BigDecimal::from(value: '10');

Money::from(value: $bigNumber, currency: $currency);

Money::fromFloat(value: 10.00, currency: 'BRL');

Money::fromString(value: '10.00', currency: 'BRL');

use TinyBlocks\Currency\Currency;

$augend = Money::fromString(value: '100', currency: 'BRL');
$addend = Money::fromString(value: '1.50', currency: Currency::BRL->value);

$result = $augend->add(addend: $addend);
$result->amount->toString();

# Output: 101.50

use TinyBlocks\Currency\Currency;

$minuend = Money::fromString(value: '10.50', currency: 'EUR');
$subtrahend = Money::fromString(value: '0.50', currency: Currency::EUR->value);

$result = $minuend->subtract(subtrahend: $subtrahend);
$result->amount->toString();

# Output: 10.00

use TinyBlocks\Currency\Currency;

$multiplicand = Money::fromString(value: '5', currency: 'GBP');
$multiplier = Money::fromString(value: '3.12', currency: Currency::GBP->value);

$result = $multiplicand->multiply(multiplier: $multiplier);
$result->amount->toString(); 

# Output: 15.60

use TinyBlocks\Currency\Currency;

$dividend = Money::fromString(value: '8.99', currency: 'CHF');
$divisor = Money::fromString(value: '5', currency: Currency::CHF->value);

$result = $dividend->divide(divisor: $divisor);
$result->amount->toString();

# Output: 1.79