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:
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 $displayDecimals = 2;
|
||||||
protected int $rounding = 2;
|
protected int $rounding = 2;
|
||||||
protected string $prefix = '$';
|
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. */
|
/** 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();
|
||||||
}
|
}
|
||||||
|
|
@ -111,7 +116,7 @@ class Currency implements Arrayable, JsonSerializable
|
||||||
|
|
||||||
/** Get the currency's math decimal count. */
|
/** Get the currency's math decimal count. */
|
||||||
public function displayDecimals(): int
|
public function displayDecimals(): int
|
||||||
{
|
{
|
||||||
return $this->displayDecimals;
|
return $this->displayDecimals;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -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,
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -23,11 +23,12 @@ class PriceFormatter
|
||||||
$currency->thousandsSeparator(),
|
$currency->thousandsSeparator(),
|
||||||
);
|
);
|
||||||
|
|
||||||
// Remove trailing zeros from price
|
if ($currency->deleteTrailingDecimalZeros()) {
|
||||||
$decimal = rtrim($decimal, '0');
|
// Remove trailing zeros from price
|
||||||
// If there is no more decimal values, remove the trailing decimal separator as well
|
$decimal = rtrim($decimal, '0');
|
||||||
$decimal = rtrim($decimal, $currency->decimalSeparator());
|
// 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();
|
return $currency->prefix() . $decimal . $currency->suffix();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue