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
// an example of floating-point inaccuracy
var_dump(0.1 + 0.2 == 0.3); // bool(false)
// for more details see:
// The Floating-Point Guide - https://floating-point-gui.de/
use CodeDistortion\Currency\Currency;
$cur1 = new Currency(5555.55, 'USD'); // normal instantiation
$cur1 = Currency::new(5555.55, 'USD'); // static instantiation which is more readable when chaining
$cur2 = $cur1->add(4444.44); // (it's immutable so a new object is created)
$cur2->between(8000, 10000); // true
print $cur2->format(); // "$9,999.99"
Currency::new(5, 'USD'); // ok - $5 USD
Currency::new(5); // InvalidCurrencyException: "Currency-code was not specified. Please pass one or specify a default"
var_dump(Currency::getDefaultCurCode()); // null
Currency::setDefaultCurCode('JPY');
print Currency::getDefaultCurCode(); // 'JPY'
Currency::new(5, 'USD'); // ok - $5 USD
Currency::new(5); // ok - $5 JPY
$cur1 = Currency::new(5); // the amount is set to $5.00 upon instantiation
$cur2 = $cur1->val(10); // and is then set to $10.00 (it's immutable so a new object is created)
$cur1 = Currency::new(5); // an integer
$cur2 = Currency::new(5.5); // a float
$cur3 = Currency::new('6.78'); // a numeric string
$cur4 = Currency::new($cur3); // another Currency object
$cur5 = Currency::new(null); // null
$cur6 = Currency::new(); // (will default to null)
$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)
$cur = Currency::new();
$cur = $cur->locale('en-US'); // sets the locale this object uses (see the 'locale' section below)
$cur = $cur->curCode('NZD'); // change the currency used
$cur = $cur->customDecPl(30); // sets the number of decimal places used (see the 'precision (custom decimal places)' section below)
$cur = $cur->useCurrencyDecPl(); // uses the current currency's decimal places again
$cur = $cur->immutable(false); // sets whether this object is immutable or not (see the 'immutability' section below)
$cur = $cur->formatSettings('!thousands'); // alters the default options used when format() is called (see the 'formatting output' section below)
$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)
$cur = Currency::new(1);
print $cur->curCode; // USD (the current currency code)
print $cur->symbol; // $ (the currency symbol in the current locale)
print $cur->decPl; // 2 (the number of decimal places in the current currency)
$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
print Currency::symbol('USD'); // "$" (will pick-up the current default locale 'en')
print Currency::symbol('USD', 'en-US'); // "$" (for a specific locale)
print Currency::symbol('USD', 'en-IN'); // "US$" (same currency, but a different symbol)
print Currency::symbol('USD', 'en-AU'); // "USD"
print Currency::symbol('JPY', 'en-US'); // "¥"
print Currency::symbol('JPY', 'ja-JP'); // "¥"
$cur = Currency::new(5);
$cur = $cur->inc(); // increment
$cur = $cur->dec(); // decrement
$cur = $cur->add(2); // add x
$cur = $cur->sub(2); // subtract x
$cur = $cur->div(2); // divide by x
$cur = $cur->mul(2); // multiply by x
$cur = $cur->round(); // round to zero decimal places
$cur = $cur->round(2); // round to x decimal places
$cur = $cur->floor(); // use the floor of the current value
$cur = $cur->ceil(); // use the ceiling of the current value
Currency::new(5)->lessThan(10); // alias of lt(..)
Currency::new(5)->lessThanOrEqualTo(10); // alias of lte(..)
Currency::new(5)->equalTo(10); // alias of eq(..)
Currency::new(5)->greaterThanOrEqualTo(10); // alias of gte(..)
Currency::new(5)->greaterThan(10); // alias of gt(..)
$cur1 = Currency::new(5);
$cur2 = Currency::new(10);
$cur1->lt($cur2); // you can compare a Currency with others
Currency::new(5)->lt(10, 15, 20); // will return true if 5 is less-than 10, 15 and 20
Currency::new(5)->between(2, 8); // check if 5 is between x and y (inclusively)
Currency::new(5)->between(2, 8, false); // check if 5 is between x and y (NOT inclusively)
$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"
$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"