From 9d2fdc205d1a6d2326532cc7c10953e6dc5e2c9a Mon Sep 17 00:00:00 2001 From: Maxim Babichev Date: Fri, 5 Jan 2024 10:25:41 +0300 Subject: [PATCH] add FormatterServiceInterface --- src/Services/FormatterService.php | 24 ++++++++++ src/Services/FormatterServiceInterface.php | 15 ++++++ tests/Units/Service/FormatterTest.php | 55 ++++++++++++++++++++++ 3 files changed, 94 insertions(+) create mode 100644 src/Services/FormatterService.php create mode 100644 src/Services/FormatterServiceInterface.php create mode 100644 tests/Units/Service/FormatterTest.php diff --git a/src/Services/FormatterService.php b/src/Services/FormatterService.php new file mode 100644 index 000000000..b078cfd14 --- /dev/null +++ b/src/Services/FormatterService.php @@ -0,0 +1,24 @@ +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); + } +} diff --git a/src/Services/FormatterServiceInterface.php b/src/Services/FormatterServiceInterface.php new file mode 100644 index 000000000..e2d8eee2f --- /dev/null +++ b/src/Services/FormatterServiceInterface.php @@ -0,0 +1,15 @@ +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); + } +}