Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Marins's avatar

Currency format

Hello,

in a blade view, I am showing currency to the user. Like this:

 {{$mobility->travelcosts}}<br>

and result comes with this format: 11250.00 I would really like the format in this way 11.250,00

Is there a way to achieve this?

Thank you

0 likes
16 replies
Snapey's avatar
Snapey
Best Answer
Level 122

assuming the value in the database is a decimal format (you gave no clue) then you can use php number format functions

http://php.net/manual/en/function.number-format.php

{{ number_format($mobility->travelcosts, 2, ',', '.') }}

there are also currency format methods linked on the same page

12 likes
gopalkumar315's avatar

For currency format, you should try this one. I did not use it but hope it works as you needed.

https://www.php.net/manual/en/function.money-format.php

Also for using in your blade file you can try this method.

You can add a custom Blade directive in the boot() method in your AppServiceProvider file.

Blade::directive('money', function ($amount) { return ""; });

// in blade @money($yourVariable)

1 like
blackdeer's avatar

To expand on @gopalkumar315:

In app\Providers\AppServiceProvider.php

Add

use Illuminate\Support\Facades\Blade;

at the top and then

Blade::directive('money', function ($amount) {
    return "<?php echo '$' . number_format($amount, 2); ?>";
});

Inside the boot() method. Use directive @money() in your blade templates.

6 likes
chas688's avatar

@blackdeer Love this. If you are using negative numbers with parenthesis, I did something like this:

Blade::directive('currency', function ($amount) {
                return "<?php echo ($amount < 0) ? str_replace('-', '', '($' . number_format($amount, 2) .')') : '$' . number_format($amount, 2)  ?>";
        });
ahoi's avatar

money_format is deprecated on PHP 7.4, so I'd not use this :-)

2 likes
Snapey's avatar

And this is a two year old thread, so thanks.

hosch's avatar

laravel-money, which @claybitner has posted, has its own peculiarities.

https://www.php.net/manual/en/function.money-format.php is deprecated since PHP 7.4 but is replaced by https://www.php.net/manual/en/numberformatter.formatcurrency.php

So I've written a simple helper function.

Create the file app/Helpers/helpers.php.

My simple function:

<?php

/**
 * Format an amount to the given currency
 *
 * @return response()
 */

if (! function_exists('formatCurrency')) {
    function formatCurrency($amount, $currency)
    {
        $fmt = new NumberFormatter( app()->getLocale(), NumberFormatter::CURRENCY );
        return $fmt->formatCurrency($amount, $currency);
    }
}

an in the composer.json add the "files" Part to autoload

"autoload": {
        "psr-4": {
            "App\": "app/",
            "Database\Factories\": "database/factories/",
            "Database\Seeders\": "database/seeders/"
        },
        "files": [
            "app/Helpers/helpers.php"
        ]
    },

Run composer dump-autoload once and you can use your formatCurrency helper function.

Works for me and maybe it helps someone.

martinbean's avatar

To add a fresh take on this, I personally use a Blade component for rendering monetary amounts. The implementation was stolen inspired by Cashier:

namespace App\View\Components;

use Illuminate\Support\Str;
use Illuminate\View\Component;
use Money\Currencies\ISOCurrencies;
use Money\Currency;
use Money\Formatter\IntlMoneyFormatter;
use Money\Money;
use NumberFormatter;

class FormatAmount extends Component
{
    public $amount;
    public $currency;
    public $locale;

    public function __construct(int $amount, string $currency, string $locale = null)
    {
        $this->amount = $amount;
        $this->currency = $currency;
        $this->locale = $locale ?? app()->getLocale();
    }

    public function render()
    {
        $money = new Money($this->amount, new Currency(Str::upper($this->currency)));

        $numberFormatter = new NumberFormatter($this->locale, NumberFormatter::CURRENCY);
        $moneyFormatter = new IntlMoneyFormatter($numberFormatter, new ISOCurrencies());

        return $moneyFormatter->format($money);
    }
}

You can then print formatted currencies in your Blade views like this:

<p>
    The price is:
    <x-format-amount amount="925" currency="usd" />
</p>

Or if you have say, a Product model with price and currency attributes:

<p>
    The price is:
    <x-format-amount :amount="$product->price" :currency="$product->currency" />
</p>
4 likes
brunofunnie's avatar

@martinbean The Money lib expects that you always pass a cents based amount. So if you store your values as decimal or float you'll need to convert it, just multiply by a hundred. Which also means that for some specific applications that needs a higher precision you should avoid this lib.

martinbean's avatar

@brunofunnie It’s a good job I do store monetary values in as an integer in its lowest denominator, then. But thanks for creating an account and replying to a thread that was created over six years ago.

Please or to participate in this conversation.