PHP code example of ecommerce-utilities / money
1. Go to this page and download the library: Download ecommerce-utilities/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/ */
ecommerce-utilities / money example snippets
use Ecommerce\Money;
// Create
$price = Money::fromValue(19.99); // Default: 2 decimal places
$tax = Money::fromValue(1.90, 2); // explicit precision
$zero = Money::zero(); // 0.00
// Arithmetic
$total = Money::add($price, $tax); // 21.89
$rest = Money::sub($total, 5); // 16.89
$multi = Money::mul($rest, 3); // 50.67
$share = Money::div($multi, 4); // 12.67 (with 2 decimals)
$rounded = Money::round('19.995', 2); // 20.00
// Comparisons
$isSame = Money::isEqual(10, '10.00'); // true
$isLeftGt = Money::isLeftSideGreater(5, 7); // true (5 < 7)
$isRightGt = Money::isRightSideGreater(9.99, 9.98); // true (9.99 > 9.98)
// Access and string representation
$asFloat = $total->getValue(); // float 21.89
$dp = $total->getDecimalPrecision(); // int 2
echo (string) $total; // "21.89"
use Ecommerce\Money;
$a = Money::fromValue(1.2345, 4);
$b = 2;
$sumDefault = Money::add($a, $b); // uses 4 from the Money operand -> "3.2345"
$sumForced = Money::add($a, $b, 3); // overrides -> "3.235"
Money::round('1.235'); // "1.24"
Money::round('-1.235'); // "-1.24"
Money::round('149.5', 0); // "150"
Money::round('150', -2); // "200", with 0 decimal places
Money::round(null, 3); // "0.000"
use Ecommerce\Money;
Money::add(null, 10); // 10.00
Money::add(null, null); // 0.00 (Money::zero())
Money::sub(null, 5); // 0.00
Money::mul(10, null); // 0.00
Money::div(10, 0); // 10.00 (division by 0 returns the left operand)