PHP code example of phpexpertsinc / money-type
1. Go to this page and download the library: Download phpexpertsinc/money-type 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/ */
phpexpertsinc / money-type example snippets
use PHPExperts\MoneyType\Money;
$money = new Money(5.22);
$money->add(0.55);
echo "$money\n"; // 5.77
# It keeps precision much much better than mere cents.
$money->subtract(0.0001);
echo "$money\n"; // 5.77
$money->subtract(0.004);
echo "$money\n"; // 5.77
$money->subtract(0.001);
echo "$money\n"; // 5.76
# Actually, to the tenth decimal place.
# So it's Cryptocurrency-ready!
$money->multiply(55.7773);
echo "$money\n"; // 321.55
# But deep down, it stores the true values to many decimals (if you use BCMath).
echo $money->getWithFullPrecision() . "\n"; // 321.5508738395
$money->divide('1.000005');
echo "$money\n"; // 321.55
echo $money->getWithFullPrecision() . "\n"; // 321.5492660931
# You can also compare really large numbers with one another, up to 10 decimal places.
# PHP Experts' MoneyType is cryptocurrency ready! In fact, that's what it was designed for!
$money->compare(321.5492660931); // 0 = equal
$money->compare(321.549266093009); // -1 = less
$money->compare(321.5492660931000001); // 1 = more
# Get the object.
print_r($money);
# It is cryptocurrency ready:
# Converts Bitcoins to Satoshis
$btc = '1.55527331';
$satoshis = NumberHelper::convertToCents($btc, 8); // 155527331 (int)