PHP

Format Money with Currency

admin by @admin ADMIN
5d ago
May 31, 2026
Public
0 0 up · 0 down Sign in to vote
Format an amount of cents as a localized currency string using NumberFormatter from intl. Falls back to a basic sprintf if intl isn't available.
PHP
Raw
<?php
function formatMoney(int $cents, string $currency = 'USD', string $locale = 'en_US'): string {
    if (class_exists(NumberFormatter::class)) {
        $f = new NumberFormatter($locale, NumberFormatter::CURRENCY);
        return $f->formatCurrency($cents / 100, $currency);
    }
    return number_format($cents / 100, 2) . ' ' . $currency;
}

echo formatMoney(199, 'USD');                    // $1.99
echo formatMoney(1599, 'EUR', 'de_DE');          // 15,99 €
echo formatMoney(50000, 'JPY', 'ja_JP');         // ¥500 (JPY has no minor unit)
Tags

Save your own code snippets

Create a free account and build your private vault. Share publicly whenever you want.