Format Currency

Format a number as currency with the appropriate symbol and separators.

Code

Utilities
$symbols = ['USD' => '$', 'EUR' => '€', 'GBP' => '£', 'JPY' => '¥'];
$symbol = $symbols[$currency] ?? $currency;
return $symbol . number_format($num, 2);

Parameters

The number to format.

Currency code (USD, EUR, GBP, etc.).

Locale for formatting.

Server

More PHP Snippets