Skip to content

Commit

Permalink
Round amount to two digits to circumvent problematic float values
Browse files Browse the repository at this point in the history
Fixes #31
  • Loading branch information
ravage84 committed Feb 17, 2020
1 parent c622948 commit 2f08a2e
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
- Removed unfinished support for Red Payment Slip (ES)

### Fixed
- Round amount to two digits to circumvent problematic float values

## [0.12.2](https://github.com/ravage84/SwissPaymentSlip/releases/tag/0.12.2) - 2017-08-07
### Added
Expand Down
6 changes: 5 additions & 1 deletion src/PaymentSlipData.php
Original file line number Diff line number Diff line change
Expand Up @@ -618,7 +618,11 @@ public function setAmount($amount = 0.0)
if (!$this->getWithAmount()) {
throw new DisabledDataException('amount');
}
$this->amount = $amount;
if (is_numeric($amount)) {
$this->amount = round($amount, 2);
} else {
$this->amount = $amount;
}

return $this;
}
Expand Down
76 changes: 76 additions & 0 deletions tests/OrangePaymentSlipDataTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -378,4 +378,80 @@ public function testSetNotForPaymentDisabledFields()

$this->slipData->setNotForPayment(true);
}

/**
* Tests the setAmount method with a problematic float as amount parameter
*
* It is common computer science knowledge that in some cases floats can be imprecise.
* The class should handle that by rounding it properly.
*
* @return void
* @covers ::setAmount
*/
public function testSetAmountWithProblematicFloat()
{
$amounts = array (
0 => 1.8,
1 => 11.0,
2 => 18.3,
3 => 2.3,
4 => 7.0,
5 => 10.2,
6 => 7.6,
7 => 2.3,
8 => 7.0,
9 => 6.4,
10 => 1.8,
11 => 2.6,
12 => 15.5,
13 => 1.8,
14 => 7.6,
15 => 8.7,
16 => 5.6,
17 => 7.6,
18 => 5.4,
19 => 3.1,
20 => 10.8,
21 => 2.6,
22 => 2.6,
23 => 6.5,
24 => 10.2,
25 => 47.0,
26 => 3.1,
27 => 2.6,
);

$total = 0.0;
foreach ($amounts as $amount) {
$total += $amount;
}

$this->assertSame(218, (int)$total);

$this->slipData->setAmount($total);

$this->assertSame(219, $this->slipData->getAmountFrancs());
$this->assertSame('00', $this->slipData->getAmountCents());
}

/**
* Tests the setAmount method with a another problematic float as amount parameter
*
* It is common computer science knowledge that in some cases floats can be imprecise.
* The class should handle that by rounding it properly.
*
* @return void
* @covers ::setAmount
*/
public function testSetAmountWithAnotherProblematicFloat()
{
$total = 114.9984;

$this->assertSame(114, (int)$total);

$this->slipData->setAmount($total);

$this->assertSame(115, $this->slipData->getAmountFrancs());
$this->assertSame('00', $this->slipData->getAmountCents());
}
}

0 comments on commit 2f08a2e

Please sign in to comment.