Skip to content

Commit 69d9a9e

Browse files
authored
Merge pull request #98 from bladeroot/HP-2446
HP-2446: format number as string
1 parent 9220708 commit 69d9a9e

File tree

2 files changed

+39
-2
lines changed

2 files changed

+39
-2
lines changed

src/price/ProgressivePrice.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,8 @@ public function calculateSum(QuantityInterface $quantity): ?Money
105105
$price = $threshold->price();
106106

107107
$chargedAmount = $price->money()
108-
->multiply((string)$billedUsage->getQuantity())
109-
->divide((string)($price->multiplier()));
108+
->multiply((string)(sprintf('%.14F', $billedUsage->getQuantity())))
109+
->divide((string)(sprintf('%.14F', $price->multiplier())));
110110

111111
$this->calculationTraces[] = new ProgressivePriceCalculationTrace(
112112
$threshold, $billedUsage, $chargedAmount

tests/unit/price/ProgressivePriceTest.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,4 +215,41 @@ private function progressivePriceProvider(): Generator
215215
],
216216
];
217217
}
218+
219+
/**
220+
* @dataProvider progressivePriceProviderSmallUsage
221+
*/
222+
public function testProgressivePriceSmallUsage(
223+
array $inputThresholdsArray,
224+
int $expectedAmount,
225+
string $startPrice,
226+
string $prepaid = '0'
227+
): void {
228+
$price = $this->createProgressivePrice(
229+
prepaid: $prepaid,
230+
startPrice: $startPrice,
231+
thresholdsArray: $inputThresholdsArray
232+
);
233+
$this->usage = Quantity::bps(6043);
234+
$usage = $price->calculateUsage($this->usage);
235+
$this->assertSame($this->usage->getQuantity(), $usage->getQuantity());
236+
237+
$amount = $price->calculateSum($this->usage);
238+
$this->assertEquals($expectedAmount, $amount->getAmount());
239+
}
240+
241+
private function progressivePriceProviderSmallUsage(): Generator
242+
{
243+
yield 'Simple case' => [
244+
'thresholds' => [
245+
['price' => '10', 'currency' => 'EUR', 'quantity' => '0', 'unit' => 'gbps'],
246+
],
247+
'money' => 0,
248+
'price' => '1',
249+
'prepaid' => '0',
250+
'trace' => [
251+
'6043bps * 0.00000001 = 0.00',
252+
],
253+
];
254+
}
218255
}

0 commit comments

Comments
 (0)