mirror of
https://github.com/archtechx/money.git
synced 2025-12-12 03:14:03 +00:00
Add setting for how to handle trailing decimal zeros
This commit is contained in:
parent
b7093f4f05
commit
8fc9d4d455
3 changed files with 20 additions and 6 deletions
|
|
@ -15,4 +15,5 @@ class USD extends Currency
|
|||
protected int $displayDecimals = 2;
|
||||
protected int $rounding = 2;
|
||||
protected string $prefix = '$';
|
||||
protected bool $deleteTrailingDecimalZeros = false;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -40,6 +40,9 @@ class Currency implements Arrayable, JsonSerializable
|
|||
/** How many decimals of the currency's values should get rounded. */
|
||||
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. */
|
||||
public function __construct(
|
||||
string $code = null,
|
||||
|
|
@ -52,6 +55,7 @@ class Currency implements Arrayable, JsonSerializable
|
|||
int $rounding = null,
|
||||
string $decimalSeparator = null,
|
||||
string $thousandsSeparator = null,
|
||||
bool $deleteTrailingDecimalZeros = null,
|
||||
) {
|
||||
$this->code = $code ?? $this->code ?? '';
|
||||
$this->name = $name ?? $this->name ?? '';
|
||||
|
|
@ -63,6 +67,7 @@ class Currency implements Arrayable, JsonSerializable
|
|||
$this->decimalSeparator = $decimalSeparator ?? $this->decimalSeparator ?? '.';
|
||||
$this->thousandsSeparator = $thousandsSeparator ?? $this->thousandsSeparator ?? ',';
|
||||
$this->rounding = $rounding ?? $this->rounding ?? $this->mathDecimals;
|
||||
$this->deleteTrailingDecimalZeros = $deleteTrailingDecimalZeros ?? $this->deleteTrailingDecimalZeros ?? false;
|
||||
|
||||
$this->check();
|
||||
}
|
||||
|
|
@ -111,7 +116,7 @@ class Currency implements Arrayable, JsonSerializable
|
|||
|
||||
/** Get the currency's math decimal count. */
|
||||
public function displayDecimals(): int
|
||||
{
|
||||
{
|
||||
return $this->displayDecimals;
|
||||
}
|
||||
|
||||
|
|
@ -133,6 +138,12 @@ class Currency implements Arrayable, JsonSerializable
|
|||
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). */
|
||||
public function __toString()
|
||||
{
|
||||
|
|
@ -153,6 +164,7 @@ class Currency implements Arrayable, JsonSerializable
|
|||
'rounding' => $this->rounding,
|
||||
'decimalSeparator' => $this->decimalSeparator,
|
||||
'thousandsSeparator' => $this->thousandsSeparator,
|
||||
'deleteTrailingDecimalZeros' => $this->deleteTrailingDecimalZeros,
|
||||
];
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -23,11 +23,12 @@ class PriceFormatter
|
|||
$currency->thousandsSeparator(),
|
||||
);
|
||||
|
||||
// Remove trailing zeros from price
|
||||
$decimal = rtrim($decimal, '0');
|
||||
// If there is no more decimal values, remove the trailing decimal separator as well
|
||||
$decimal = rtrim($decimal, $currency->decimalSeparator());
|
||||
|
||||
if ($currency->deleteTrailingDecimalZeros()) {
|
||||
// Remove trailing zeros from price
|
||||
$decimal = rtrim($decimal, '0');
|
||||
// If there is no more decimal values, remove the trailing decimal separator as well
|
||||
$decimal = rtrim($decimal, $currency->decimalSeparator());
|
||||
}
|
||||
|
||||
return $currency->prefix() . $decimal . $currency->suffix();
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue