- Output for 8.0.17, 8.1.23 - 8.1.28, 8.2.10 - 8.2.19, 8.3.0 - 8.3.7
- 1337.9876 1337.9876
<?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;