generated from boboko/starter
price function global (needs review)
This commit is contained in:
@@ -0,0 +1,29 @@
|
||||
<?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, ',', '.');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user