Skip to content

Commit

Permalink
Merge pull request #15 from smartbooster/add_convert_cents_euro
Browse files Browse the repository at this point in the history
@lfortunier Add `MathUtils::convertCentsToEuro` : Convert cents to euro
  • Loading branch information
mathieu-ducrot authored Feb 27, 2024
2 parents b13ff37 + 9c592c4 commit 6714cfb
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 1 deletion.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
CHANGELOG for 1.x
===================
## v1.0.4 - (2024-02-27)

### Added

- `MathUtils::convertCentsToEuro` : Convert cents to euro

## v1.0.3 - (2024-01-11)

### Fix
Expand Down
2 changes: 1 addition & 1 deletion src/Query/MySQL/GroupConcat.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public function parse(\Doctrine\ORM\Query\Parser $parser): void
}

if ($lexer->isNextToken(Lexer::T_IDENTIFIER)) {
if (strtolower($lexer->lookahead['value']) !== 'separator') {
if (strtolower($lexer->lookahead['value']) !== 'separator') { // @phpstan-ignore-line
$parser->syntaxError('separator');
}
$parser->match(Lexer::T_IDENTIFIER);
Expand Down
8 changes: 8 additions & 0 deletions src/Utils/MathUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,12 @@ public static function formatBytes(float $size, int $precision = 2): string
$floor = floor($base);
return round(pow(1000, $base - $floor), $precision) . ' ' . $suffixes[$floor];
}

public static function convertCentsToEuro(?float $price): float
{
if (null === $price) {
return 0.0;
}
return $price / 100;
}
}
17 changes: 17 additions & 0 deletions tests/Utils/MathUtilsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Smart\CoreBundle\Tests\Utils;

use App\Utils\NumberUtils;
use Smart\CoreBundle\Utils\MathUtils;
use PHPUnit\Framework\TestCase;

Expand Down Expand Up @@ -68,4 +69,20 @@ public function formatBytesProvider(): array
'420.96 TB' => ['420.96 TB', 420956000000000, 2],
];
}

/** @dataProvider convertCentsToEuroProvider */
public function testConvertCentsToEuro(float $expected, float $price): void
{
$this->assertSame($expected, MathUtils::convertCentsToEuro($price));
}

public function convertCentsToEuroProvider(): array
{
return [
'simple' => [5, 500],
'float' => [5.54, 554],
'little' => [0.01, 1],
'price_float' => [5.893121, 589.3121],
];
}
}

0 comments on commit 6714cfb

Please sign in to comment.