aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/brick/math/src/BigDecimal.php
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/brick/math/src/BigDecimal.php')
-rw-r--r--vendor/brick/math/src/BigDecimal.php38
1 files changed, 38 insertions, 0 deletions
diff --git a/vendor/brick/math/src/BigDecimal.php b/vendor/brick/math/src/BigDecimal.php
index 31d22ab30..21d1c4ae3 100644
--- a/vendor/brick/math/src/BigDecimal.php
+++ b/vendor/brick/math/src/BigDecimal.php
@@ -8,6 +8,7 @@ use Brick\Math\Exception\DivisionByZeroException;
use Brick\Math\Exception\MathException;
use Brick\Math\Exception\NegativeNumberException;
use Brick\Math\Internal\Calculator;
+use Override;
/**
* Immutable, arbitrary-precision signed decimal numbers.
@@ -47,6 +48,7 @@ final class BigDecimal extends BigNumber
/**
* @psalm-pure
*/
+ #[Override]
protected static function from(BigNumber $number): static
{
return $number->toBigDecimal();
@@ -535,6 +537,7 @@ final class BigDecimal extends BigNumber
return new BigDecimal(Calculator::get()->neg($this->value), $this->scale);
}
+ #[Override]
public function compareTo(BigNumber|int|float|string $that) : int
{
$that = BigNumber::of($that);
@@ -552,6 +555,7 @@ final class BigDecimal extends BigNumber
return - $that->compareTo($this);
}
+ #[Override]
public function getSign() : int
{
return ($this->value === '0') ? 0 : (($this->value[0] === '-') ? -1 : 1);
@@ -568,6 +572,33 @@ final class BigDecimal extends BigNumber
}
/**
+ * Returns the number of significant digits in the number.
+ *
+ * This is the number of digits to both sides of the decimal point, stripped of leading zeros.
+ * The sign has no impact on the result.
+ *
+ * Examples:
+ * 0 => 0
+ * 0.0 => 0
+ * 123 => 3
+ * 123.456 => 6
+ * 0.00123 => 3
+ * 0.0012300 => 5
+ */
+ public function getPrecision(): int
+ {
+ $value = $this->value;
+
+ if ($value === '0') {
+ return 0;
+ }
+
+ $length = \strlen($value);
+
+ return ($value[0] === '-') ? $length - 1 : $length;
+ }
+
+ /**
* Returns a string representing the integral part of this decimal number.
*
* Example: `-123.456` => `-123`.
@@ -609,6 +640,7 @@ final class BigDecimal extends BigNumber
return $this->getFractionalPart() !== \str_repeat('0', $this->scale);
}
+ #[Override]
public function toBigInteger() : BigInteger
{
$zeroScaleDecimal = $this->scale === 0 ? $this : $this->dividedBy(1, 0);
@@ -616,11 +648,13 @@ final class BigDecimal extends BigNumber
return self::newBigInteger($zeroScaleDecimal->value);
}
+ #[Override]
public function toBigDecimal() : BigDecimal
{
return $this;
}
+ #[Override]
public function toBigRational() : BigRational
{
$numerator = self::newBigInteger($this->value);
@@ -629,6 +663,7 @@ final class BigDecimal extends BigNumber
return self::newBigRational($numerator, $denominator, false);
}
+ #[Override]
public function toScale(int $scale, RoundingMode $roundingMode = RoundingMode::UNNECESSARY) : BigDecimal
{
if ($scale === $this->scale) {
@@ -638,16 +673,19 @@ final class BigDecimal extends BigNumber
return $this->dividedBy(BigDecimal::one(), $scale, $roundingMode);
}
+ #[Override]
public function toInt() : int
{
return $this->toBigInteger()->toInt();
}
+ #[Override]
public function toFloat() : float
{
return (float) (string) $this;
}
+ #[Override]
public function __toString() : string
{
if ($this->scale === 0) {