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.

<span class="line"><span style="color: #F97583">public</span><span style="color: #E1E4E8"> </span><span style="color: #F97583">function</span><span style="color: #E1E4E8"> </span><span style="color: #B392F0">currency</span><span style="color: #E1E4E8">(</span><span style="color: #F97583">String</span><span style="color: #E1E4E8"> $currency </span><span style="color: #F97583">=</span><span style="color: #E1E4E8"> </span><span style="color: #9ECBFF">'USD'</span><span style="color: #E1E4E8">)</span></span>
<span class="line"></span>

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.

<span class="line"><span style="color: #F97583">public</span><span style="color: #E1E4E8"> </span><span style="color: #F97583">function</span><span style="color: #E1E4E8"> </span><span style="color: #B392F0">format</span><span style="color: #E1E4E8">(</span><span style="color: #F97583">Float</span><span style="color: #E1E4E8"> $money, </span><span style="color: #F97583">Bool</span><span style="color: #E1E4E8"> $zeroDecimals </span><span style="color: #F97583">=</span><span style="color: #E1E4E8"> </span><span style="color: #79B8FF">false</span><span style="color: #E1E4E8">)</span><span style="color: #F97583">:</span><span style="color: #E1E4E8"> </span><span style="color: #F97583">String</span></span>
<span class="line"></span>

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:

<span class="line"><span style="color: #F97583">use</span><span style="color: #E1E4E8"> </span><span style="color: #79B8FF">Magarrent\LaravelCurrencyFormatter\Currency</span><span style="color: #E1E4E8">;</span></span>
<span class="line"></span>
<span class="line"><span style="color: #E1E4E8">$currencyFormatter </span><span style="color: #F97583">=</span><span style="color: #E1E4E8"> </span><span style="color: #F97583">new</span><span style="color: #E1E4E8"> </span><span style="color: #79B8FF">Currency</span><span style="color: #E1E4E8">();</span></span>
<span class="line"><span style="color: #79B8FF">echo</span><span style="color: #E1E4E8"> $currencyFormatter</span><span style="color: #F97583">-></span><span style="color: #B392F0">currency</span><span style="color: #E1E4E8">(</span><span style="color: #9ECBFF">'USD'</span><span style="color: #E1E4E8">)</span><span style="color: #F97583">-></span><span style="color: #B392F0">format</span><span style="color: #E1E4E8">(</span><span style="color: #79B8FF">1234.56</span><span style="color: #E1E4E8">);  </span><span style="color: #6A737D">// Outputs: $1,234.56</span></span>
<span class="line"></span>

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.

<span class="line"><span style="color: #F97583">public</span><span style="color: #E1E4E8"> </span><span style="color: #F97583">function</span><span style="color: #E1E4E8"> </span><span style="color: #B392F0">setSymbol</span><span style="color: #E1E4E8">(</span><span style="color: #F97583">String</span><span style="color: #E1E4E8"> $customSymbol)</span></span>
<span class="line"></span>

Parameters:

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

Example Usage:

<span class="line"><span style="color: #E1E4E8">$currencyFormatter</span><span style="color: #F97583">-></span><span style="color: #B392F0">setSymbol</span><span style="color: #E1E4E8">(</span><span style="color: #9ECBFF">'ยค'</span><span style="color: #E1E4E8">)</span><span style="color: #F97583">-></span><span style="color: #B392F0">format</span><span style="color: #E1E4E8">(</span><span style="color: #79B8FF">1234.56</span><span style="color: #E1E4E8">);  </span><span style="color: #6A737D">// Outputs: ยค1,234.56</span></span>
<span class="line"></span>

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.