Using number_format method in Laravel
Stefan Bogdanescu
Founder & Senior Architect · 2026-06-29
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.
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$Expensesand displaying various elements like type, narration, price, quantity, and amount using Blade syntax. You attempted to usenumber_format($Expense->amount), but it didn't work as expected.Getting Started with number_format:
Thenumber_formatfunction 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_formatwill automatically adjust the precision of the decimal places based on your currency's requirements.Using number_format in Laravel Blade:
To usenumber_formatin 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) <tr> <!-- other table cells --> <td>{{ number_format($Expense->price, 2, '.', ',') }}</td> <!-- other table cells --> </tr> @endforeachCurrency Formatting with a Custom Symbol:
To format currency values, you can use the third argument ofnumber_formatto set your currency symbol and decimal separator:php@foreach ($Expenses as $Expense) <tr> <!-- other table cells --> <td style="text-align: right;">{{ number_format($Expense->amount, 2, '.', ',') . '€' }}</td> <!-- other table cells --> </tr> @endforeachConclusion:
With the knowledge you have gained in this comprehensive guide, you can now effectively apply Laravel'snumber_formatmethod 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 asmoney_format()or third-party libraries like Laravel Sanctum for formatting dates, numbers, and other types of data.Final Code:
The updated code for your view would look something like this:php@foreach ($Expenses as $Expense) <tr> <!-- other table cells --> <td style="text-align: right;">{{ number_format($Expense->price, 2, '.', ',') }}</td> <td style="text-align: center;">{{ $Expense->quantity }}</td> <!-- other table cells --> </tr> @endforeachFinal 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.