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); + } +}