Skip to content

Commit 42cb836

Browse files
authored
Merge pull request #44 from ravage84/circumvent-problematic-floats
Round amount to two digits to circumvent problematic float values
2 parents 3f5e235 + 2f08a2e commit 42cb836

File tree

3 files changed

+82
-1
lines changed

3 files changed

+82
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
1010
- Removed unfinished support for Red Payment Slip (ES)
1111

1212
### Fixed
13+
- Round amount to two digits to circumvent problematic float values
1314

1415
## [0.12.2](https://github.com/ravage84/SwissPaymentSlip/releases/tag/0.12.2) - 2017-08-07
1516
### Added

src/PaymentSlipData.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -618,7 +618,11 @@ public function setAmount($amount = 0.0)
618618
if (!$this->getWithAmount()) {
619619
throw new DisabledDataException('amount');
620620
}
621-
$this->amount = $amount;
621+
if (is_numeric($amount)) {
622+
$this->amount = round($amount, 2);
623+
} else {
624+
$this->amount = $amount;
625+
}
622626

623627
return $this;
624628
}

tests/OrangePaymentSlipDataTest.php

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,4 +378,80 @@ public function testSetNotForPaymentDisabledFields()
378378

379379
$this->slipData->setNotForPayment(true);
380380
}
381+
382+
/**
383+
* Tests the setAmount method with a problematic float as amount parameter
384+
*
385+
* It is common computer science knowledge that in some cases floats can be imprecise.
386+
* The class should handle that by rounding it properly.
387+
*
388+
* @return void
389+
* @covers ::setAmount
390+
*/
391+
public function testSetAmountWithProblematicFloat()
392+
{
393+
$amounts = array (
394+
0 => 1.8,
395+
1 => 11.0,
396+
2 => 18.3,
397+
3 => 2.3,
398+
4 => 7.0,
399+
5 => 10.2,
400+
6 => 7.6,
401+
7 => 2.3,
402+
8 => 7.0,
403+
9 => 6.4,
404+
10 => 1.8,
405+
11 => 2.6,
406+
12 => 15.5,
407+
13 => 1.8,
408+
14 => 7.6,
409+
15 => 8.7,
410+
16 => 5.6,
411+
17 => 7.6,
412+
18 => 5.4,
413+
19 => 3.1,
414+
20 => 10.8,
415+
21 => 2.6,
416+
22 => 2.6,
417+
23 => 6.5,
418+
24 => 10.2,
419+
25 => 47.0,
420+
26 => 3.1,
421+
27 => 2.6,
422+
);
423+
424+
$total = 0.0;
425+
foreach ($amounts as $amount) {
426+
$total += $amount;
427+
}
428+
429+
$this->assertSame(218, (int)$total);
430+
431+
$this->slipData->setAmount($total);
432+
433+
$this->assertSame(219, $this->slipData->getAmountFrancs());
434+
$this->assertSame('00', $this->slipData->getAmountCents());
435+
}
436+
437+
/**
438+
* Tests the setAmount method with a another problematic float as amount parameter
439+
*
440+
* It is common computer science knowledge that in some cases floats can be imprecise.
441+
* The class should handle that by rounding it properly.
442+
*
443+
* @return void
444+
* @covers ::setAmount
445+
*/
446+
public function testSetAmountWithAnotherProblematicFloat()
447+
{
448+
$total = 114.9984;
449+
450+
$this->assertSame(114, (int)$total);
451+
452+
$this->slipData->setAmount($total);
453+
454+
$this->assertSame(115, $this->slipData->getAmountFrancs());
455+
$this->assertSame('00', $this->slipData->getAmountCents());
456+
}
381457
}

0 commit comments

Comments
 (0)