PHP code example of archtechx / money
1. Go to this page and download the library: Download archtechx/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/ */
archtechx / money example snippets
// Incorrect
$money = money(1500);
$money->times(3); // ❌
$money->value(); // 1500
// Correct
$money = money(1500);
$money = $money->times(3); // ✅
$money->value(); // 4500
// Using cents
$money = money(1500); // $15.00; default currency
$money = money(1500, 'EUR'); // 15.00 €
$money = money(2000, new USD); // $20.00
$money = money(3000, CZK::class); // 30 Kč
// Using decimals
$money = Money::fromDecimal(15.00, 'EUR'); // 15.00 €
$money = Money::fromDecimal(20.00, new USD); // $20.00
$money = Money::fromDecimal(30.00, CZK::class); // 30 Kč
// Addition
$money = money(1000);
$money = $money->add(500);
$money->value(); // 1500
// Subtraction
$money = money(1000);
$money = $money->subtract(500);
$money->value(); // 500
// Multiplication
$money = money(1000);
$money = $money->multiplyBy(2); // alias: ->times()
$money->value(); // 2000
// Division
$money = money(1000);
$money = $money->divideBy(2);
$money->value(); // 500
$money = money(2200);
$money->convertTo(CZK::class);
// Assuming CZK is 25:1 USD
// ✅ true
money(100, USD::class)->equals(money(100, USD::class));
// ❌ false
money(100, USD::class)->equals(money(200, USD::class));
// ✅ true
money(100, USD::class)->equals(money(2500, CZK::class));
// ❌ false
money(100, USD::class)->equals(money(200, CZK::class));
// Assuming CZK is 25:1 USD
// ✅ true
money(100, USD::class)->is(money(100, USD::class));
// ❌ false: different monetary value
money(100, USD::class)->is(money(200, USD::class));
// ❌ false: different currency
money(100, USD::class)->is(money(2500, CZK::class));
// ❌ false: different currency AND monetary value
money(100, USD::class)->is(money(200, CZK::class));
$money = money(1000);
$money = $money->addTax(20.0); // 20%
$money->value(); // 1200
$money = Money::fromDecimal(100.0, new USD);
$money->value(); // 10000
$money->decimal(); // 100.0
$money = Money::fromDecimal(40.25, USD::class);
$money->formatted(); // $40.25
$money = Money::fromDecimal(40.25, USD::class);
// $ 40.25 USD
$money->formatted(decimalSeparator: ',', prefix: '$ ', suffix: ' USD');
$money = Money::fromDecimal(40.25, USD::class);
// $ 40.25 USD
$money->formatted(['decimalSeparator' => ',', 'prefix' => '$ ', 'suffix' => ' USD']);
$money = Money::new(123456, CZK::class);
$money->rawFormatted(); // 1 234,56 Kč
$money = money(1000);
$formatted = $money->formatted(); // $10.00
$fromFormatted = Money::fromFormatted($formatted);
$fromFormatted->is($money); // true
$money = money(1000);
$formatted = $money->formatted(['prefix' => '$ ', 'suffix' => ' USD']); // $ 10.00 USD
$fromFormatted = Money::fromFormatted($formatted, USD::class, ['prefix' => '$ ', 'suffix' => ' USD']);
$fromFormatted->is($money); // true
$money = Money::fromDecimal(3.30, CZK::class);
$money->value(); // 330
$money->formatted(); // 3 Kč
$money = $money->times(3);
$money->value(); // 990
$money->formatted(); // 10 Kč
$money = Money::fromDecimal(9.90, CZK::class);
$money->decimal(); // 9.90
$money->formatted(); // 10 Kč
$money->rounding(); // +0.10 Kč = 10
$money = Money::fromDecimal(3.30, CZK::class);
$money->decimal(); // 3.30
$money->formatted(); // 3 Kč
$money->rounding(); // -0.30 Kč = -30
// Using the currency rounding
$money = Money::fromDecimal(9.90, CZK::class);
$money->decimal(); // 9.90
$money = $money->rounded(); // currency rounding
$money->decimal(); // 10.0
// Using custom rounding
$money = Money::fromDecimal(2.22, USD::class);
$money->decimal(); // 2.22
$money = $money->rounded(1); // custom rounding: 1 decimal
$money->decimal(); // 2.20
// anonymous Currency object
$currency = new Currency(
code: 'FOO',
name: 'Foo currency',
rate: 1.8,
prefix: '# ',
suffix: ' FOO',
);
// array
$currency = [
'code' => 'FOO',
'name' => 'Foo currency',
'rate' => 1.8,
'prefix' => '# ',
'suffix' => ' FOO',
];
// class
class FOO extends Currency
{
protected string $code = 'FOO';
protected string $name = 'Foo currency';
protected float $rate = 1.8;
protected string $prefix = '# ';
protected string $suffix = ' FOO';
}
currencies()->add(new USD);
currencies()->add(USD::class);
currencies()->add($currency); // object or array
currencies()->remove('USD');
currencies()->remove(USD::class);
currencies()->clear();
currencies()->reset();
protected string $code = null;
protected string $name = null;
protected float $rate = null;
protected string $prefix = null;
protected string $suffix = null;
protected int $mathDecimals = null;
protected int $displayDecimals = null;
protected int $rounding = null;
protected string $decimalSeparator = null;
protected string $thousandsSeparator = null;
public function rate(): float
{
return cache()->remember("{$this->code}.rate", 3600, function () {
return Http::get("https://api.currency.service/rate/USD/{$this->code}");
});
}
currencies()->setDefault('USD');
currencies()->setCurrent('USD');
currencies()
->storeCurrentUsing(fn (string $code) => session()->put('currency', $code))
->resolveCurrentUsing(fn () => session()->get('currency'));
Route::get('/currency/change/{currency}', function (string $currency) {
currencies()->setCurrent($currency);
return redirect()->back();
});
$money = money(1000);
$money->value(); // 1000
$money = Money::fromDecimal(100.0); // $100 USD
$money->value(); // 10000
$money->decimal(); // 100.0
money(123456, new CZK)->formatted(); // 1 235 Kč
money(123456, new CZK)->rawFormatted(); // 1 234,56 Kč
class EditProduct extends Component
{
public Money $price;
// ...
}
$currency = new CZK;
$json = json_encode($currency);
$currency = Currency::fromJson($json);
$foo = money(100, 'CZK');
$bar = Money::fromJson($money->toJson());
$money->is($bar); // true
currency(USD::class);
currency(new USD);
currency('USD');
money(1000, USD::class)->convertTo('CZK');
money(1000, 'USD')->convertTo(new CZK);
money(1000, new USD)->convertTo(CZK::class);
// LoadCurrencies middleware
currencies()->add(cache()->remember('currencies', 3600, function () {
return UserCurrencies::where('user_id', auth()->id())->get()->toArray();
});