-
-
Notifications
You must be signed in to change notification settings - Fork 237
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
94 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Bavix\Wallet\Services; | ||
|
||
use Brick\Math\BigDecimal; | ||
use Brick\Math\RoundingMode; | ||
|
||
final readonly class FormatterService implements FormatterServiceInterface | ||
{ | ||
public function intValue(string|int|float $amount, int $decimalPlaces): string | ||
{ | ||
return (string) BigDecimal::ten() | ||
->power($decimalPlaces) | ||
->multipliedBy(BigDecimal::of($amount)) | ||
->toScale(0, RoundingMode::DOWN); | ||
} | ||
|
||
public function floatValue(string|int|float $amount, int $decimalPlaces): string | ||
{ | ||
return (string) BigDecimal::ofUnscaledValue($amount, $decimalPlaces); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Bavix\Wallet\Services; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
interface FormatterServiceInterface | ||
{ | ||
public function intValue(string|int|float $amount, int $decimalPlaces): string; | ||
|
||
public function floatValue(string|int|float $amount, int $decimalPlaces): string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Bavix\Wallet\Test\Units\Service; | ||
|
||
use Bavix\Wallet\Internal\Exceptions\ExceptionInterface; | ||
use Bavix\Wallet\Services\FormatterServiceInterface; | ||
use Bavix\Wallet\Test\Infra\TestCase; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
final class FormatterTest extends TestCase | ||
{ | ||
/** | ||
* @throws ExceptionInterface | ||
*/ | ||
public function testFloatValueDP3(): void | ||
{ | ||
$result = app(FormatterServiceInterface::class)->floatValue('12345', 3); | ||
|
||
self::assertSame('12.345', $result); | ||
} | ||
|
||
/** | ||
* @throws ExceptionInterface | ||
*/ | ||
public function testFloatValueDP2(): void | ||
{ | ||
$result = app(FormatterServiceInterface::class)->floatValue('12345', 2); | ||
|
||
self::assertSame('123.45', $result); | ||
} | ||
|
||
/** | ||
* @throws ExceptionInterface | ||
*/ | ||
public function testIntValueDP3(): void | ||
{ | ||
$result = app(FormatterServiceInterface::class)->intValue('12.345', 3); | ||
|
||
self::assertSame('12345', $result); | ||
} | ||
|
||
/** | ||
* @throws ExceptionInterface | ||
*/ | ||
public function testIntValueDP2(): void | ||
{ | ||
$result = app(FormatterServiceInterface::class)->intValue('123.45', 2); | ||
|
||
self::assertSame('12345', $result); | ||
} | ||
} |