Skip to content

Type Casts added #22

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
28 changes: 14 additions & 14 deletions src/Decimal.php
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ public function compareTo($value): int
$decimal = static::create($value);
$scale = max($this->scale(), $decimal->scale());

return bccomp($this, $decimal, $scale);
return bccomp((string)$this, (string)$decimal, $scale);
}

/**
Expand All @@ -258,7 +258,7 @@ public function add($value, ?int $scale = null)
$decimal = static::create($value);
$scale = $this->resultScale($this, $decimal, $scale);

return new static(bcadd($this, $decimal, $scale));
return new static(bcadd((string)$this, (string)$decimal, $scale));
}

/**
Expand Down Expand Up @@ -296,7 +296,7 @@ public function subtract($value, ?int $scale = null)
$decimal = static::create($value);
$scale = $this->resultScale($this, $decimal, $scale);

return new static(bcsub($this, $decimal, $scale));
return new static(bcsub((string)$this, (string)$decimal, $scale));
}

/**
Expand Down Expand Up @@ -406,7 +406,7 @@ public function multiply($value, ?int $scale = null)
$scale = $this->scale() + $decimal->scale();
}

return new static(bcmul($this, $decimal, $scale));
return new static(bcmul((string)$this, (string)$decimal, $scale));
}

/**
Expand All @@ -426,7 +426,7 @@ public function divide($value, int $scale)
throw new DivisionByZeroError('Cannot divide by zero. Only Chuck Norris can!');
}

return new static(bcdiv($this, $decimal, $scale));
return new static(bcdiv((string)$this, (string)$decimal, $scale));
}

/**
Expand All @@ -443,7 +443,7 @@ public function pow($exponent, ?int $scale = null)
$scale = $this->scale();
}

return new static(bcpow($this, (string)$exponent, $scale));
return new static(bcpow((string)$this, (string)$exponent, $scale));
}

/**
Expand All @@ -459,7 +459,7 @@ public function sqrt(?int $scale = null)
$scale = $this->scale();
}

return new static(bcsqrt($this, $scale));
return new static(bcsqrt((string)$this, $scale));
}

/**
Expand All @@ -476,10 +476,10 @@ public function mod($value, ?int $scale = null)
$scale = $this->scale();
}
if (version_compare(PHP_VERSION, '7.2') < 0) {
return new static(bcmod($this, (string)$value));
return new static(bcmod((string)$this, (string)$value));
}

return new static(bcmod($this, (string)$value, $scale));
return new static(bcmod((string)$this, (string)$value, $scale));
}

/**
Expand All @@ -495,16 +495,16 @@ public function round(int $scale = 0, int $roundMode = self::ROUND_HALF_UP)
$e = bcpow('10', (string)$exponent);
switch ($roundMode) {
case static::ROUND_FLOOR:
$v = bcdiv(bcadd(bcmul($this, $e, 0), $this->isNegative() ? '-9' : '0'), $e, 0);
$v = bcdiv(bcadd(bcmul((string)$this, $e, 0), $this->isNegative() ? '-9' : '0'), $e, 0);

break;
case static::ROUND_CEIL:
$v = bcdiv(bcadd(bcmul($this, $e, 0), $this->isNegative() ? '0' : '9'), $e, 0);
$v = bcdiv(bcadd(bcmul((string)$this, $e, 0), $this->isNegative() ? '0' : '9'), $e, 0);

break;
case static::ROUND_HALF_UP:
default:
$v = bcdiv(bcadd(bcmul($this, $e, 0), $this->isNegative() ? '-5' : '5'), $e, $scale);
$v = bcdiv(bcadd(bcmul((string)$this, $e, 0), $this->isNegative() ? '-5' : '5'), $e, $scale);
}

return new static($v);
Expand Down Expand Up @@ -831,14 +831,14 @@ protected function fromScientific(string $value, ?int $scale): void
} else {
$this->integralPart = bcmul($matches[2], bcpow('10', (string)$exp));

$pos = strlen((string)$this->integralPart);
$pos = strlen($this->integralPart);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we remove the string cast here, shouldnt we also remove it in line 841?

$this->fractionalPart = str_pad($this->fractionalPart, $scale - strlen((string)$this->integralPart), '0');

if (strpos($value, '.') !== false) {
$pos++;
}
$this->fractionalPart = rtrim(substr($value, $pos), '.');

if ($scale !== null) {
$this->fractionalPart = str_pad($this->fractionalPart, $scale - strlen((string)$this->integralPart), '0');
$this->fractionalPart = str_pad($this->fractionalPart, $scale - strlen($this->integralPart), '0');
}
}
}
Expand Down