PHP code example of whallysson / money-precision

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

    

whallysson / money-precision example snippets




use Whallysson\Money\Money;

echo Money::of(10086)->decimal() . PHP_EOL; // Output: 100.86
echo Money::of(5660)->decimal() . PHP_EOL; // Output: 56.60
echo Money::of(100.86)->int() . PHP_EOL; // Output: 10086
echo Money::of('56.60')->int() . PHP_EOL; // Output: 5660



use Whallysson\Money\Money;

$money1 = Money::of(100.86);
$money2 = Money::of(56.60);

echo $money1->add($money2)->decimal() . PHP_EOL; // Output: 157.46
echo $money1->sub($money2)->decimal() . PHP_EOL; // Output: 44.26
echo $money1->mul(2)->decimal() . PHP_EOL; // Output: 201.72
echo $money1->div(2)->decimal() . PHP_EOL; // Output: 50.43



use Whallysson\Money\Money;

$money1 = Money::of(100.86);
$money2 = Money::of(56.60);

var_dump([
    'equals' => $money1->equals($money2), // false
    'greaterThan' => $money1->greaterThan($money2), // true
    'lessThan' => $money1->lessThan($money2), // false
]);



use Whallysson\Money\Money;

echo Money::of(100.86)->format() . PHP_EOL; // Output: R$ 100,86
echo Money::of(56.60)->format('USD') . PHP_EOL; // Output: $ 56.60
echo Money::of(356.78->format('EUR') . PHP_EOL; // Output: 356,78 €