3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php /** * Immutable class for handling numbers with precision. * * Never change the $number or the $precision after the creation of the object. */ class DecimalNumber { private $precision; private $number; /** * Get an instance given a string and a precision * @param string $string The decimal number in string form * @param int $precision The precision of the number * @return DecimalNumber Return new instance */ public static function get($string, $precision = 4) { $me = get_called_class(); $number = new $me; $number->precision = $precision; $number->number = bcadd($string, '0', $precision); return $number; } /** * Add a formal entry point for decimal numbers * @param DecimalNumber $number Decimal number to base off of * @param integer $precision new precision * @return DecimalNumber DecimalNumber with new precision */ public static function decimalNumber(DecimalNumber $number, $precision = 4) { if ($number->precision === $precision) return clone $number; return static::get($number, $precision); } /** * Perform an addition operation * @param DecimalNumber $number Addend * @return DecimalNumber Sum */ public function sum(DecimalNumber $number) { return DecimalNumber::get(bcadd($this->number, $number, $this->precision)); } /** * Perform a subtraction operation * @param DecimalNumber $number Addend * @return DecimalNumber Sum */ public function difference(DecimalNumber $number) { return DecimalNumber::string(bcsub($this->number, $number, $this->precision)); } /** * Perform a multiplication operation * @param DecimalNumber $number Multiplier * @return DecimalNumber Product */ public function product(DecimalNumber $number) { return DecimalNumber::get(bcmul($this->number, $number, $this->precision)); } /** * Perform a division operation * @param DecimalNumber $number Divisor * @return DecimalNumber Quotient */ public function quotient(DecimalNumber $number) { return DecimalNumber::get(bcdiv($this->number, $number, $this->precision)); } public function floored() { return DecimalNumber::get(bcadd(bcadd($this->number, '0'), '0', $this->precision), $this->precision); } /** * Automagically convert to a string * @return string returns a string containing the value of this DecimalNumber */ public function __toString() { return $this->number; } } $a = DecimalNumber::get('.7'); $b = DecimalNumber::get('.1'); $c = $a->add($b); $d = $c->product(DecimalNumber::get('10'));
Output for 5.3.0 - 5.4.21
Fatal error: Constructor DecimalNumber::decimalNumber() cannot be static in /in/mfHQe on line 86
Process exited with code 255.