Skip to content

Open-source event management and ticket selling platform — perfect for concerts, conferences, and everything in between 🎟️ If you find this project helpful, please consider giving us a star ⭐️

1.3 KB
Raw Blame
<?php

namespace HiEvents\Helper;

use NumberFormatter;

class Currency
{
    private const ZERO_DECIMAL_CURRENCIES = [
        'BIF',
        'CLP',
        'DJF',
        'GNF',
        'JPY',
        'KMF',
        'KRW',
        'MGA',
        'PYG',
        'RWF',
        'UGX',
        'VND',
        'VUV',
        'XAF',
        'XOF',
        'XPF',
    ];

    public static function isZeroDecimalCurrency(string $currencyCode): bool
    {
        return in_array(strtoupper($currencyCode), self::ZERO_DECIMAL_CURRENCIES, true);
    }

    public static function format(float|int $amount, string $currencyCode, string $locale = 'en_US'): string
    {
        $currencyCode = strtoupper($currencyCode);
        $formatter = new NumberFormatter($locale.'@currency='.$currencyCode, NumberFormatter::CURRENCY);

        return $formatter->formatCurrency($amount, $currencyCode);
    }

    public static function fromMinorUnits(int $minorUnits, string $currencyCode): float
    {
        return $minorUnits / (self::isZeroDecimalCurrency($currencyCode) ? 1 : 100);
    }

    public static function round(float $value, $precision = 2): float
    {
        return round(
            num: $value,
            precision: $precision,
            mode: PHP_ROUND_HALF_UP
        );
    }
}