3v4l.org

run code in 300+ PHP versions simultaneously
<?php class Decimal { private string $value; public function __construct(string $value) { $this->value = $value; } public function toFixed(int $scale): string { // rough example for adjusting precision - do not use return bcadd($this->value, '0', $scale); } } class Entity { #[ORM\Column(type: 'decimal', precision: 10, scale: 4)] private string $subtotal = '0.0'; // name as desired public function getSubtotalDecimal(): Decimal { return new Decimal($this->subtotal, 4); } public function getSubtotal(): string { return $this->subtotal; } // see Union Types public function setSubtotal(string|Decimal $subtotal): self { if ($subtotal instanceof Decimal) { $subtotal = $subtotal->toFixed(4); } $this->subtotal = $subtotal; return $this; } } $decimal = new Decimal("1337.987654321"); $entity = new Entity(); $entity->setSubtotal($decimal); echo $entity->getSubtotal() . \PHP_EOL; echo $entity->getSubtotalDecimal()->toFixed(4) . \PHP_EOL;
Output for git.master, git.master_jit, rfc.property-hooks
1337.9876 1337.9876

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:
57.37 ms | 405 KiB | 5 Q