3v4l.org

run code in 300+ PHP versions simultaneously
<?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 string($string, $precision = 4) { $me = get_called_class(); $number = new $me; $number->precision = $precision; $number->number = bcadd($string, '0', $precision); return $number; } /** * Perform an addition operation * @param DecimalNumber $number the number to add * @return DecimalNumber Return the sum */ public function sum(DecimalNumber $number) { return DecimalNumber::string(bcadd($this->number, $number, $this->precision)); } public function floored() { return bcadd(bcadd($this->number, '0'), '0', $this->precision); } /** * Automagically convert to a string * @return string returns a string containing the value of this DecimalNumber */ public function __toString() { return $this->number; } } $d = DecimalNumber::string('4.7'); echo $d->sum(DecimalNumber::string('2.1')); echo $d->floored();
Output for git.master, git.master_jit, rfc.property-hooks
6.80004.0000

This tab shows result from various feature-branches currently under review by the php developers. Contact me to have additional branches featured.

Active branches

Archived branches

Once feature-branches are merged or declined, they are no longer available. Their functionality (when merged) can be viewed from the main output page


preferences:
48.5 ms | 401 KiB | 8 Q