Laravel Currency Formatter - Technical Documentation

๐Ÿš€ Usage - Formatting a Currency

The Laravel Currency Formatter package enables developers to format monetary values according to various global currencies effortlessly using Laravel. Below outlines the steps to utilize the Currency class for selecting a currency, formatting monetary values, including methods to customize the currency symbol.

Selecting a Currency

To select a currency for formatting, utilize the currency() method of the Currency class.

public function currency(String $currency = 'USD')

Parameters:

  • string $currency: Pass the currency code (e.g., 'USD', 'EUR', 'BTC') that you want to select.

Formatting a Value

To format a monetary value according to the selected currency, use the format() method.

public function format(Float $money, Bool $zeroDecimals = false): String

Parameters:

  • Float $money: The monetary value you want to format.
  • boolean $zeroDecimals (default = false): If set to true, the formatted value will not display decimal digits.

Example Usage:

use Magarrent\LaravelCurrencyFormatter\Currency;

$currencyFormatter = new Currency();
echo $currencyFormatter->currency('USD')->format(1234.56);  // Outputs: $1,234.56

This will output the monetary value 1234.56 formatted as per the USD currency formatting rules.

Customizing the Currency Symbol

If you want to customize the currency symbol, you can use the setSymbol() method.

public function setSymbol(String $customSymbol)

Parameters:

  • String $customSymbol: The new symbol to use for the current currency.

Example Usage:

$currencyFormatter->setSymbol('ยค')->format(1234.56);  // Outputs: ยค1,234.56

Note: Customizing the currency symbol does not affect the formatting rules (such as decimal and thousand separators) for the currency.

Additional Notes:

  • The currency options (symbol, decimal digits, separators, etc.) are defined in src/resources/Currencies.php.
  • An Exception is thrown if an unsupported currency is passed to the currency() method.
  • The package utilizes the Laravel Service Provider pattern to register the currency service.

Contributors who wish to add more currencies or improve functionality should refer to the contributing guidelines of the project repository.


This documentation section demonstrates how to incorporate currency formatting capabilities within a Laravel application, utilizing the Currency class provided by the Laravel Currency Formatter package.