Skip to content

Commit 637dd69

Browse files
committed
ACP2E-2022: [On-prem] Zend Currency deprecated so no custom price formatting works like in old framework
1 parent 2d2dbcb commit 637dd69

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

lib/internal/Magento/Framework/Currency/Data/Currency.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,10 @@ public function toCurrency($value = null, array $options = []): string
167167
}
168168
$options = array_merge($this->options, $this->checkOptions($options));
169169
$numberFormatter = new NumberFormatter($options['locale'], NumberFormatter::CURRENCY);
170+
if (isset($options['precision'])) {
171+
$numberFormatter->setAttribute(NumberFormatter::FRACTION_DIGITS, $options['precision']);
172+
}
173+
170174
$value = $numberFormatter->format((float) $value);
171175

172176
if (is_numeric($options['display']) === false) {

lib/internal/Magento/Framework/Test/Unit/Currency/Data/CurrencyTest.php

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ class CurrencyTest extends TestCase
2323
* @return void
2424
* @throws \Magento\Framework\Currency\Exception\CurrencyException
2525
*
26-
* @dataProvider toCurrencyPositionsDataProvider
26+
* @dataProvider optionsDataProvider
2727
*/
28-
public function testToCurrencyPositions(float|int|null $value, array $options, string $expectedResult): void
28+
public function testToCurrencyWithOptions(float|int|null $value, array $options, string $expectedResult): void
2929
{
3030
$currency = new Currency();
3131
$result = $currency->toCurrency($value, $options);
@@ -36,7 +36,7 @@ public function testToCurrencyPositions(float|int|null $value, array $options, s
3636
/**
3737
* @return array[]
3838
*/
39-
public function toCurrencyPositionsDataProvider(): array
39+
public function optionsDataProvider(): array
4040
{
4141
return [
4242
'rightPosition_en_AU' => [
@@ -102,6 +102,24 @@ public function toCurrencyPositionsDataProvider(): array
102102
],
103103
'expectedResult' => 'CUST 12.00',
104104
],
105+
'precisionIsGreaterThanZero' => [
106+
'value' => 12.16,
107+
'options' => [
108+
'locale' => 'en_US',
109+
'currency' => 'USD',
110+
'precision'=> 1,
111+
],
112+
'expectedResult' => '$12.2',
113+
],
114+
'precisionIsZero' => [
115+
'value' => 12.16,
116+
'options' => [
117+
'locale' => 'en_US',
118+
'currency' => 'USD',
119+
'precision'=> 0,
120+
],
121+
'expectedResult' => '$12',
122+
],
105123
];
106124
}
107125
}

0 commit comments

Comments
 (0)