1
0
Fork 0
mirror of https://github.com/archtechx/money.git synced 2025-12-12 11:24:03 +00:00

Add setting for how to handle trailing decimal zeros

This commit is contained in:
David 2022-05-06 11:35:41 +02:00
parent b7093f4f05
commit 8fc9d4d455
No known key found for this signature in database
GPG key ID: EC68B85E411F4753
3 changed files with 20 additions and 6 deletions

View file

@ -15,4 +15,5 @@ class USD extends Currency
protected int $displayDecimals = 2; protected int $displayDecimals = 2;
protected int $rounding = 2; protected int $rounding = 2;
protected string $prefix = '$'; protected string $prefix = '$';
protected bool $deleteTrailingDecimalZeros = false;
} }

View file

@ -40,6 +40,9 @@ class Currency implements Arrayable, JsonSerializable
/** How many decimals of the currency's values should get rounded. */ /** How many decimals of the currency's values should get rounded. */
protected int $rounding; protected int $rounding;
/** Setting to decide if trailing decimal zeros should be removed for when calling formatted(). */
protected bool $deleteTrailingDecimalZeros;
/** Create a new Currency instance. */ /** Create a new Currency instance. */
public function __construct( public function __construct(
string $code = null, string $code = null,
@ -52,6 +55,7 @@ class Currency implements Arrayable, JsonSerializable
int $rounding = null, int $rounding = null,
string $decimalSeparator = null, string $decimalSeparator = null,
string $thousandsSeparator = null, string $thousandsSeparator = null,
bool $deleteTrailingDecimalZeros = null,
) { ) {
$this->code = $code ?? $this->code ?? ''; $this->code = $code ?? $this->code ?? '';
$this->name = $name ?? $this->name ?? ''; $this->name = $name ?? $this->name ?? '';
@ -63,6 +67,7 @@ class Currency implements Arrayable, JsonSerializable
$this->decimalSeparator = $decimalSeparator ?? $this->decimalSeparator ?? '.'; $this->decimalSeparator = $decimalSeparator ?? $this->decimalSeparator ?? '.';
$this->thousandsSeparator = $thousandsSeparator ?? $this->thousandsSeparator ?? ','; $this->thousandsSeparator = $thousandsSeparator ?? $this->thousandsSeparator ?? ',';
$this->rounding = $rounding ?? $this->rounding ?? $this->mathDecimals; $this->rounding = $rounding ?? $this->rounding ?? $this->mathDecimals;
$this->deleteTrailingDecimalZeros = $deleteTrailingDecimalZeros ?? $this->deleteTrailingDecimalZeros ?? false;
$this->check(); $this->check();
} }
@ -133,6 +138,12 @@ class Currency implements Arrayable, JsonSerializable
return $this->rounding; return $this->rounding;
} }
/** Get the setting for delete trailing decimal zeros. */
public function deleteTrailingDecimalZeros(): bool
{
return $this->deleteTrailingDecimalZeros;
}
/** Convert the currency to a string (returns the code). */ /** Convert the currency to a string (returns the code). */
public function __toString() public function __toString()
{ {
@ -153,6 +164,7 @@ class Currency implements Arrayable, JsonSerializable
'rounding' => $this->rounding, 'rounding' => $this->rounding,
'decimalSeparator' => $this->decimalSeparator, 'decimalSeparator' => $this->decimalSeparator,
'thousandsSeparator' => $this->thousandsSeparator, 'thousandsSeparator' => $this->thousandsSeparator,
'deleteTrailingDecimalZeros' => $this->deleteTrailingDecimalZeros,
]; ];
} }

View file

@ -23,11 +23,12 @@ class PriceFormatter
$currency->thousandsSeparator(), $currency->thousandsSeparator(),
); );
if ($currency->deleteTrailingDecimalZeros()) {
// Remove trailing zeros from price // Remove trailing zeros from price
$decimal = rtrim($decimal, '0'); $decimal = rtrim($decimal, '0');
// If there is no more decimal values, remove the trailing decimal separator as well // If there is no more decimal values, remove the trailing decimal separator as well
$decimal = rtrim($decimal, $currency->decimalSeparator()); $decimal = rtrim($decimal, $currency->decimalSeparator());
}
return $currency->prefix() . $decimal . $currency->suffix(); return $currency->prefix() . $decimal . $currency->suffix();
} }