35 lines
904 B
PHP
35 lines
904 B
PHP
|
|
<?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, '.', ',');
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|