Home >Backend Development >PHP Tutorial >Currency Formatting with Laravel's Enhanced Number Helper
Laravel's Number Assistant now supports configurable default currencies, simplifying price formatting across different regions and use cases. This enhancement is especially valuable for international customers or applications that handle multi-currency transactions. Without the need to manually manage currency symbols and formatting, you can now use Laravel’s built-in formatting capabilities along with configurable default values.
use Illuminate\Support\Number; // 设置应用程序范围的默认值 Number::useCurrency('EUR'); // 使用默认值格式化 $price = Number::currency(1000); // €1,000.00 // 临时覆盖 $usdPrice = Number::currency(1000, in: 'USD'); // ,000.00
The following is an example of implementing a multi-region 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 ) ]; } }
Number Assistant's enhanced currency formatting feature simplifies multi-currency support in Laravel applications. It provides flexibility for application-wide default values and context-specific currency formatting, making it easier to handle international pricing requirements.
The above is the detailed content of Currency Formatting with Laravel's Enhanced Number Helper. For more information, please follow other related articles on the PHP Chinese website!