init
This commit is contained in:
34
web10/app/Support/NumberFormatter.php
Normal file
34
web10/app/Support/NumberFormatter.php
Normal file
@@ -0,0 +1,34 @@
|
||||
<?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, '.', ',');
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user