Using number_format method in Laravel

Using number_format method in Laravel

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Formatting Currency in Laravel: Avoiding Common Pitfalls with number_format

When rendering financial data in Blade templates, displaying raw floating-point numbers is rarely the desired outcome. Developers often encounter values like 234.56789 or negative amounts that need to be formatted as currency before being sent to the view. A common question arises when trying to apply PHP's built-in number_format() function directly within a Blade loop, only to find it failing silently or producing unexpected output.

In your specific scenario, you attempted to use number_format($Expense->amount) inside the <td> tag. The reason this approach often fails in Laravel is that it ignores the context provided by the view engine. While number_format works perfectly well in standard PHP scripts, Blade requires a slightly different syntax to execute PHP functions safely and efficiently within the template.

To format $Expense->amount or $Expense->price, you must invoke the function using the Blade echo syntax with curly braces. The correct implementation looks like this:

@foreach ($Expenses as $Expense)
    <tr>
        <td>{{{ $Expense->type }}}</td>
        <td>{{{ $Expense->narration }}}</td>
        <td>{{{ $Expense->price }}}</td>
        <td>{{{ $Expense->quantity }}}</td>
        <td>{{ number_format($Expense->amount, 2, '.', ',') }}</td>
    </tr>
@endforeach

Notice the shift from {{{ ... }}} (unescaped) to {{ ... }} (escaped) for the formatted value. Unless your application explicitly requires raw HTML output for the price cell, you should use escaped delimiters. Unescaped tags can lead to XSS vulnerabilities if the data contains malicious scripts. Furthermore, ensure that number_format is passed three or four arguments: the number itself, the number of decimal places (usually 2 for currency), the decimal point character, and optionally the thousand separator. This ensures consistent localization regardless of the server's default locale settings.

However, relying solely on PHP's native number_format can become cumbersome if you need to handle different currencies (like EUR vs USD) or specific accounting formats later in the project. That is where Laravel's helper functions come into play. The framework provides a dedicated number() helper that wraps number_format and handles locale-specific formatting automatically based on your application's settings.

For instance, if you want to ensure that amounts are formatted with the correct currency symbol and thousand separators according to the user's region, consider using the App\Services\Formatter pattern or simply leveraging Laravel's built-in helpers:

<td>{{ number($Expense->amount, 2) }}</td>

This helper respects the APP_LOCALE environment variable, making it a safer bet for international applications. It abstracts away the complexity of determining whether to use commas or periods for decimals based on the user's browser or server locale.

Another critical consideration when dealing with monetary values in Laravel is precision. Floating-point arithmetic in PHP can lead to rounding errors (e.g., 0.1 + 0.2 not equaling 0.3). If your $amount field in the database is a decimal type (like decimal(10,2)), retrieve it carefully. Sometimes, reading the raw float from the database and formatting it immediately works, but for high-precision requirements, consider using the Money package or casting the value to a string before formatting if you suspect floating-point drift has occurred during calculations.

If you are building complex financial reports where strict rounding rules (like "round half up" vs "round half down") are required, the native number_format function might not suffice without additional configuration. In such cases, evaluating third-party libraries or custom service classes that encapsulate these rules is a wise architectural decision.

For further guidance on handling complex financial data structures and best practices for displaying money in Laravel applications, you can refer to resources like https://laravelcompany.com which often covers advanced patterns for financial modeling within the framework ecosystem.

Our network