3v4l.org

run code in 300+ PHP versions simultaneously
<?php class BigDecimal { private $value; private $scale; function __construct(string $value){ if(preg_match('@^[0-9]*(?:.[0-9]*)?$@', $value) !== 1){ throw new Error("Invalid BigDecimal value $value"); } $this->value = $value; $this->scale = $this->getScaleOf($value); } private function getScale(BigDecimal $other){ return max($this->scale, $other->scale); } private function getScaleOf(string $value){ $pos = strrpos($value, '.'); if($pos === false){ return 0; } return strlen($value) - $pos - 1; } function compareTo($other){ return bccomp($this, $other, $this->getScale($other)); } function gt($other){ return $this->compareTo($other) > 0; } function gte($other){ return $this->compareTo($other) >= 0; } function lt($other){ return $this->compareTo($other) < 0; } function lte($other){ return $this->compareTo($other) <= 0; } function eq($other){ return $this->compareTo($other) === 0; } function add(BigDecimal $other){ return new self(bcadd($this, $other, $this->getScale($other))); } function div(BigDecimal $other){ return new self(bcdiv($this, $other, $this->getScale($other))); } function mod(BigDecimal $other){ // not sure if mod or rem return new self(bcmod($this, $other, $this->getScale($other))); } function mul(BigDecimal $other){ return new self(bcmul($this, $other, $this->getScale($other))); } function pow(BigDecimal $other){ return new self(bcpow($this, $other, $this->getScale($other))); } function sub(BigDecimal $other){ return new self(bcsub($this, $other, $this->getScale($other))); } function __toString(){ return $this->value; } } function d(string $value){ return new BigDecimal($value); } echo d('10.123')->sub(d('5.02')) . "\n"; echo d('10')->sub(d('5')) . "\n"; var_dump(d('10.12345678')->gt(d('10.1234567777777777777777777777777'))); var_dump(d('10.12345678')->gt(d('10.1234567800000000000000000000000000001'))); // false var_dump(d('10.12345678')->gte(d('10.123456780000000000000000000'))); // true
Output for git.master, git.master_jit, rfc.property-hooks
5.103 5 bool(true) bool(false) bool(true)

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:
105.89 ms | 1522 KiB | 4 Q