From f1a2c81aeba0c0a386eccf4fc6e7ec7a9bdab724 Mon Sep 17 00:00:00 2001 From: Dennis Eichhorn Date: Fri, 9 Jun 2023 17:39:31 +0000 Subject: [PATCH] continue implementation --- Stdlib/Base/FloatInt.php | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/Stdlib/Base/FloatInt.php b/Stdlib/Base/FloatInt.php index 3a658b555..5c2419307 100755 --- a/Stdlib/Base/FloatInt.php +++ b/Stdlib/Base/FloatInt.php @@ -183,6 +183,44 @@ class FloatInt implements SerializableInterface : \str_pad($left, 1, '0'); } + /** + * Get money. + * + * @param null|int $decimals Precision (null = auto decimals) + * + * @return string + * + * @throws \Exception this exception is thrown if an internal substr error occurs + * + * @since 1.0.0 + */ + public function getFloat(?int $decimals = 2) : string + { + $isNegative = $this->value < 0 ? 1 : 0; + + $value = $this->value === 0 + ? \str_repeat('0', self::MAX_DECIMALS) + : (string) \round($this->value, -self::MAX_DECIMALS + $decimals); + + $left = \substr($value, 0, -self::MAX_DECIMALS + $isNegative); + + /** @var string $left */ + $left = $left === false ? '0' : $left; + $right = \substr($value, -self::MAX_DECIMALS + $isNegative); + + if ($right === false) { + throw new \Exception(); // @codeCoverageIgnore + } + + if ($decimals === null) { + $decimals = \strlen(\rtrim($right, '0')); + } + + return $decimals > 0 + ? \number_format((float) $left, 0, $this->decimal, '') . $this->decimal . \substr($right, 0, $decimals) + : \str_pad($left, 1, '0'); + } + /** * Add money. *