Using number_format method in Laravel

Stefan Izdrail

Founder & Senior Architect · 2026-06-29

Laravel Company
Title: Formatting Currency Values with Laravel's number_format Method in Blade Templates Introduction: As a new developer working with Laravel and Blade templates, you may find yourself facing the need to format numbers such as price and amount to display correctly. In this comprehensive guide, we will show you how to use the built-in `number_format` method to achieve clean formatting for currencies within your views. 1. Aiming for a Clear Formatting with number_format: To better understand the concept of formatting numbers, let's examine the code in question and identify what needs improvement. In the provided code, you are iterating over `$Expenses` and displaying various elements like type, narration, price, quantity, and amount using Blade syntax. You attempted to use `number_format($Expense->amount)`, but it didn't work as expected. 2. Getting Started with number_format: The `number_format` function is a PHP method that formats numeric values in a given format, such as currency or percentage. It takes three arguments: the number to be formatted, the minimum number of digits after the decimal point, and the rounding rule (optional). By default, `number_format` will automatically adjust the precision of the decimal places based on your currency's requirements. 3. Using number_format in Laravel Blade: To use `number_format` in a blade template, simply wrap the variable name within curly braces and call the method directly. Here's an example of formatting the price (`$Expense->price`) with two decimal places: ```php @foreach ($Expenses as $Expense) {{ number_format($Expense->price, 2, '.', ',') }} @endforeach ``` 4. Currency Formatting with a Custom Symbol: To format currency values, you can use the third argument of `number_format` to set your currency symbol and decimal separator: ```php @foreach ($Expenses as $Expense) {{ number_format($Expense->amount, 2, '.', ',') . '€' }} @endforeach ``` 5. Conclusion: With the knowledge you have gained in this comprehensive guide, you can now effectively apply Laravel's `number_format` method to your views and format numeric values according to your needs. Remember that the best approach depends on your application logic and preferred output formatting. For complex data manipulation, consider using helper methods such as `money_format()` or third-party libraries like Laravel Sanctum for formatting dates, numbers, and other types of data. 6. Final Code: The updated code for your view would look something like this: ```php @foreach ($Expenses as $Expense) {{ number_format($Expense->price, 2, '.', ',') }} {{ $Expense->quantity }} @endforeach ``` 7. Final Note: As a Laravel developer, always remember to use the appropriate methods and best practices when working with data formatting in your applications. Keep learning new techniques and exploring the vast library of helpful resources available online. Feel free to refer to our website (https://laravelcompany.com) for more in-depth articles on Laravel development.