use Illuminate\Support\Number; // 设置应用程序范围的默认值 Number::useCurrency('EUR'); // 使用默认值格式化 $price = Number::currency(1000); // €1,000.00 // 临时覆盖 $usdPrice = Number::currency(1000, in: 'USD'); // ,000.00
<?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 ) ]; } }
Atas ialah kandungan terperinci Pemformatan mata wang dengan pembantu nombor yang dipertingkatkan Laravel '. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!