public function Fraction::fromDecimal in Fraction 7
Calculates the numerator and denominator from a decimal value.
Parameters
$value: The decimal value to start with.
Return value
Fraction Returns this Fraction object.
File
- ./
fraction.class.inc, line 188 - Fraction class
Class
- Fraction
- @file Fraction class
Code
public function fromDecimal($value) {
// Calculate the precision by counting the number of decimal places.
$precision = drupal_strlen(drupal_substr(strrchr($value, '.'), 1));
// Create the denominator by raising 10 to the power of the precision.
if (function_exists('bcpow')) {
$denominator = bcpow(10, $precision);
}
else {
$denominator = pow(10, $precision);
}
// Calculate the numerator by multiplying the value by the denominator.
if (function_exists('bcmul')) {
$numerator = bcmul($value, $denominator, 0);
}
else {
$numerator = $value * $denominator;
}
// Set the numerator and denominator.
$this
->setNumerator($numerator);
$this
->setDenominator($denominator);
return $this;
}