Formatting & currencies
format()
Section titled “format()”Currency::format(float $amount, string $currency, bool $includeSymbol = true): stringCurrency::format(1234.56, 'USD'); // "$ 1,234.56"Currency::format(1234.56, 'USD', false); // "1,234.56"Currency::format(1234.56, 'UAH'); // "1 234,56 ₴"Currency::format(1234.56, 'EUR'); // "€ 1 234,56"Currency::format(1234.56, 'XYZ'); // "XYZ 1,234.56" — not in config(Separators as in the default config.)
format() only formats — it doesn’t convert. Combine the two for prices in another currency:
Currency::format(Currency::convert($price, 'UAH', 'USD'), 'USD');How the string is built, from the currency’s entry in currencies:
number_format($amount, precision, decimalSeparator, thousandSeparator)- with the symbol:
symbol . ' ' . numberforsymbolPlacement = before,number . ' ' . symbolforafter— always one space between them
| Field | When missing from the currency’s config |
|---|---|
precision | default_precision |
thousandSeparator | , |
decimalSeparator | . |
symbol | the currency code |
symbolPlacement | before |
The amount is formatted by PHP’s number_format(): no locale, no negative-number styling (-5 gives $ -5.00). For locale-aware output use PHP intl (NumberFormatter) and keep this package for the rates.
Currencies from config
Section titled “Currencies from config”Currency::getActiveCurrencies(); // currencies without 'active' => false, keyed by codeCurrency::getActiveCurrencyCodes(); // ['EUR', 'PLN', 'UAH', 'USD']Currency::getAllCurrencies(); // the whole 'currencies' array, including 'active' => falseCurrency::getCurrencyConfig('usd'); // ['code' => 'USD', 'symbol' => '$', ...] or [] if not configuredEvery currency listed in currencies is active unless it has 'active' => false. The list is your app’s catalogue (e.g. for a currency switcher); it doesn’t limit which currencies can be converted.
Precision
Section titled “Precision”Currency::getPrecision('USD'); // the currency's 'precision', else default_precisionCurrency::getDefaultPrecision(); // config('currency.default_precision'), 2getPrecision() is what convert() rounds the result to.
Symbols
Section titled “Symbols”currency_symbol('USD'); // '$', or the code itself for a currency not in configSee Helpers & Blade.