Skip to content

Commit 39c9ac0

Browse files
authored
Add $withCurrencyCode parameter to render amount of money with currency code (#21)
1 parent 34253d3 commit 39c9ac0

File tree

3 files changed

+23
-3
lines changed

3 files changed

+23
-3
lines changed

CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ Security - in case of vulnerabilities.
1616

1717
## [Unreleased]
1818

19+
## 2.1.0 (2024-02-29)
20+
21+
### Changed
22+
23+
+ Added `$withCurrencyCode` parameter to `Money::prettyPrint()` to render amount of money with currency code.
24+
1925
## 2.0.0 (2022-02-09)
2026

2127
### Changed

src/Money.php

+8-3
Original file line numberDiff line numberDiff line change
@@ -164,16 +164,21 @@ public function getFormattedAmount(string $decimalPoint = '.', string $thousands
164164
*
165165
* @param string $decimalPoint
166166
* @param string $thousandsSeparator
167+
* @param bool $withCurrencyCode
167168
*
168169
* @return string
169170
*/
170-
public function getPrettyPrint(string $decimalPoint = '.', string $thousandsSeparator = ','): string
171-
{
171+
public function getPrettyPrint(
172+
string $decimalPoint = '.',
173+
string $thousandsSeparator = ',',
174+
bool $withCurrencyCode = false
175+
): string {
172176
$currencySign = $this->getCurrency()->getSign();
173177
$formattedAmount = ltrim($this->getFormattedAmount($decimalPoint, $thousandsSeparator), '-');
174178
$amountSign = $this->isNegative() ? '-' : '';
179+
$currencyCode = $withCurrencyCode ? " {$this->getCurrency()->getCurrencyCode()}" : '';
175180

176-
return "{$amountSign}{$currencySign}{$formattedAmount}";
181+
return "{$amountSign}{$currencySign}{$formattedAmount}{$currencyCode}";
177182
}
178183

179184
/**

tests/MoneyTest.php

+9
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,15 @@ public function testFormattedNegativeAmountCanBeRetrieved(): void
143143
self::assertSame('-€1,200.34', $m->getPrettyPrint('.', ','));
144144
}
145145

146+
public function testFormattedAmountCanBeRetrievedWithCurrecyCode(): void
147+
{
148+
$m = new Money(120034, new Currency('EUR'));
149+
self::assertSame(
150+
'€1,200.34 EUR',
151+
$m->getPrettyPrint('.', ',', true)
152+
);
153+
}
154+
146155
/**
147156
* @depends testObjectCanBeConstructedFromIntegerValueAndCurrencyObject
148157
*

0 commit comments

Comments
 (0)