You are here

public function Fraction::setDenominator in Fraction 8

Same name and namespace in other branches
  1. 2.x src/Fraction.php \Drupal\fraction\Fraction::setDenominator()

Sets the denominator.

Parameters

string|int $denominator: The denominator value.

Return value

Fraction Returns this Fraction object.

Overrides FractionInterface::setDenominator

7 calls to Fraction::setDenominator()
Fraction::add in src/Fraction.php
Adds another fraction to this one.
Fraction::divide in src/Fraction.php
Divides this fraction by another one.
Fraction::multiply in src/Fraction.php
Multiplies this fraction with another one.
Fraction::reciprocate in src/Fraction.php
Reciprocates the fraction.
Fraction::reduce in src/Fraction.php
Reduces the fraction to its simplest form.

... See full list

File

src/Fraction.php, line 86

Class

Fraction
A simple class for representing and acting upon a fraction.

Namespace

Drupal\fraction

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