コード例 #1
0
ファイル: Ratio.php プロジェクト: krixon/math
 /**
  * Converts the ratio to a Decimal of the specified scale.
  *
  * @param int|null $scale
  *
  * @return Decimal
  */
 public function toDecimal($scale = null) : Decimal
 {
     $antecedent = $this->dividend->toString();
     $consequent = $this->divisor->toString();
     if (null !== $scale) {
         return Decimal::fromString(bcdiv($antecedent, $consequent, $scale));
     }
     if (null !== $this->decimalValue) {
         return $this->decimalValue;
     }
     // No scale specified, calculate based on the default scale and then get rid of any extraneous zeros.
     // Note that usually these zeros would be considered significant, but the caller specifically requested
     // that we use the lowest possible precision decimal without losing any information from the ratio by not
     // specifying a scale.
     $decimal = bcdiv($antecedent, $consequent, self::SCALE);
     $decimal = rtrim($decimal, '0');
     $decimal = rtrim($decimal, '.');
     return $this->decimalValue = Decimal::fromString($decimal);
 }