Files
3dealer/app/Support/Price.php
T

30 lines
777 B
PHP

<?php
namespace App\Support;
class Price
{
/**
* Format a price the Greek way: comma as the decimal separator, dot as
* the thousands separator, and no decimals shown unless the amount
* actually needs them (19.00 -> "19", 19.50 -> "19,5", 19.55 -> "19,55").
*/
public static function format(float|int|string|null $amount, string $currency = '€'): ?string
{
if ($amount === null) {
return null;
}
$amount = round((float) $amount, 2);
$cents = (int) round($amount * 100);
$decimals = match (true) {
$cents % 100 === 0 => 0,
$cents % 10 === 0 => 1,
default => 2,
};
return $currency.number_format($amount, $decimals, ',', '.');
}
}