Laravel Tutorials

Currency Formatting with Laravel's Enhanced Number Helper

Published
Currency Formatting with Laravel's Enhanced Number Helper image

Laravel's Number helper now supports configurable default currencies, simplifying price formatting across different regions and use cases. This enhancement is particularly valuable for applications serving international customers or handling transactions in multiple currencies. Instead of manually managing currency symbols and formats, you can now leverage Laravel's built-in formatting with configurable defaults.

use Illuminate\Support\Number;
// Set application-wide default
Number::useCurrency('EUR');
// Format with default
$price = Number::currency(1000); // €1,000.00
// Temporary override
$usdPrice = Number::currency(1000, in: 'USD'); // $1,000.00

Here's an example of implementing a multi-regional checkout system:

<?php

namespace App\Services;

use App\Models\Order;
use Illuminate\Support\Number;

class PricingService
{
    public function formatOrderPrices(Order $order, string $displayCurrency)
    {
        return Number::withCurrency($displayCurrency, function() use ($order) {
            return [
                'subtotal' => Number::currency($order->subtotal),
                'tax' => Number::currency($order->tax),
                'shipping' => Number::currency($order->shipping_cost),
                'total' => Number::currency($order->total),
                'savings' => $this->calculateDiscounts($order)
            ];
        });
    }

    private function calculateDiscounts(Order $order): array
    {
        return [
            'bulk_discount' => Number::currency($order->bulk_discount),
            'loyalty_savings' => Number::currency($order->loyalty_discount),
            'total_saved' => Number::currency(
                $order->bulk_discount + $order->loyalty_discount
            )
        ];
    }
}

The Number helper's enhanced currency formatting capabilities streamline multi-currency support in Laravel applications. It provides flexibility for both application-wide defaults and context-specific currency formatting, making it easier to handle international pricing requirements.

Harris Raftopoulos photo

Senior Software Engineer • Staff & Educator @ Laravel News • Co-organizer @ Laravel Greece Meetup

Sponsored

serpapi logo
SerpApi

The Web Search API for Your LLM and AI Applications

Visit SerpApi

The latest

View all →
Email Sending and Testing for Laravel in One Account image

Email Sending and Testing for Laravel in One Account

Read article
Decide with Jev: Tie Every AI Check to the Exact Draft image

Decide with Jev: Tie Every AI Check to the Exact Draft

Read article
PIE 1.5 Changes PHP Extension Installs in CI image

PIE 1.5 Changes PHP Extension Installs in CI

Read article
Delete Expired Sessions in Laravel With a Scheduled Command image

Delete Expired Sessions in Laravel With a Scheduled Command

Read article
Pollora: Run WordPress Inside a Laravel Application image

Pollora: Run WordPress Inside a Laravel Application

Read article
Scheduled Session Garbage Collection in Laravel 13.39 image

Scheduled Session Garbage Collection in Laravel 13.39

Read article