1. Go to this page and download the library: Download code-distortion/currency 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/ */
code-distortion / currency example snippets
php
$cur = Currency::new()->customDecPl(20)->val(0.12345678901234567890); // "0.12345678901235" (precision lost because the number passed is a PHP float)
$cur = Currency::new()->customDecPl(20)->val('0.12345678901234567890'); // "0.12345678901234567890" (passed as a string)
php
$cur = Currency::new()->customDecPl(20)->val('0.12345678901234567890');
print $cur->val; // "0.12345678901234567890" (returned as a string, or null)
print $cur->cast; // 0.12345678901235 (cast to either an integer, float or null - this is less accurate)
php
$cur = Currency::new();
print $cur->customDecPl; // null (see the 'precision (custom decimal places)' section below)
print $cur->usingCustomDecPl; // false (see the 'precision (custom decimal places)' section below)
print $cur->locale; // "en"
print $cur->immutable; // true
php
// without customDecPl
$cur = Currency::new('0.98765'); // this has more decimal places than USD has
print $cur->val; // "0.99" (ie. rounded to the default 2 decimal places)
print $cur->decPl; // 2
print $cur->customDecPl; // null
print $cur->usingCustomDecPl; // false
// with customDecPl
$cur = Currency::new()->customDecPl(30)->val('0.123456789012345678901234567890');
print $cur->val; // "0.123456789012345678901234567890" (the full 30 decimal places)
print $cur->decPl; // 30
print $cur->customDecPl; // 30
print $cur->usingCustomDecPl; // true
php
$cur = Currency::new()->customDecPl(30);
print $cur->decPl; // 30
$cur = $cur->useCurrencyDecPl(); // goes back to USD's 2 decimal places - the amount inside will be rounded automatically
print $cur->decPl; // 2
php
$cur1 = Currency::new(1);
$cur2 = $cur1->add(2); // $cur1 remains unchanged and $cur2 is a new object containing the new value
print $cur1->format(); // "$1.00"
print $cur2->format(); // "$3.00"
php
$cur1 = Currency::new(1)->immutable(false);
$cur2 = $cur1->add(2); // $cur1 is changed and $cur2 points to the same object
print $cur1->format(); // "$3.00"
print $cur2->format(); // "$3.00"