public function Fraction::setDenominator in Fraction 7
Set the denominator.
Parameters
$value: The denominator value.
Return value
Fraction Returns this Fraction object.
8 calls to Fraction::setDenominator()
- Fraction::add in ./
fraction.class.inc - Add another fraction to this one.
- Fraction::divide in ./
fraction.class.inc - Divide this fraction by another one.
- Fraction::fromDecimal in ./
fraction.class.inc - Calculates the numerator and denominator from a decimal value.
- Fraction::multiply in ./
fraction.class.inc - Multiply this fraction with another one.
- Fraction::reciprocate in ./
fraction.class.inc - Reciprocate the fraction.
File
- ./
fraction.class.inc, line 57 - Fraction class
Class
- Fraction
- @file Fraction class
Code
public function setDenominator($value) {
// Protect against division by zero.
if (empty($value)) {
$this
->setNumerator(0);
$value = 1;
}
// Normalize negative fractions.
// If the denominator is negative, invert the signs for both numbers.
if ($value < 0) {
$numerator = $this
->getNumerator();
$numerator = $numerator * -1;
$this
->setNumerator($numerator);
$value = $value * -1;
}
// Cast the value as a string and save it.
$this->denominator = (string) $value;
return $this;
}