Files
ai-nav/web10/app/Support/NumberFormatter.php

35 lines
904 B
PHP
Raw Permalink Normal View History

2026-02-05 22:22:10 +08:00
<?php
if (!class_exists('NumberFormatter')) {
class NumberFormatter
{
public const CURRENCY = 1;
public const DECIMAL = 2;
private string $locale;
private int $style;
public function __construct(string $locale, int $style)
{
$this->locale = $locale;
$this->style = $style;
}
public function formatCurrency(float | int | string $amount, string $currency): string
{
$value = (float) $amount;
$formatted = number_format($value, 2, '.', ',');
return $currency . ' ' . $formatted;
}
public function format(float | int | string $number): string
{
$value = (float) $number;
$decimals = (abs($value - (int) $value) > 0.000001) ? 2 : 0;
return number_format($value, $decimals, '.', ',');
}
}
}