diff --git a/src/Currencies/USD.php b/src/Currencies/USD.php index ebd6714..b9d607f 100644 --- a/src/Currencies/USD.php +++ b/src/Currencies/USD.php @@ -15,4 +15,5 @@ class USD extends Currency protected int $displayDecimals = 2; protected int $rounding = 2; protected string $prefix = '$'; + protected bool $deleteTrailingDecimalZeros = false; } diff --git a/src/Currency.php b/src/Currency.php index adaf4cf..05df2f0 100644 --- a/src/Currency.php +++ b/src/Currency.php @@ -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, ]; } diff --git a/src/PriceFormatter.php b/src/PriceFormatter.php index 7768b80..89fc6a9 100644 --- a/src/PriceFormatter.php +++ b/src/PriceFormatter.php @@ -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(); }