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

This is an error 500

Value for `_results` contains invalid data `array`


preferences:
158.87 ms | 2181 KiB | 24 Q