Skip to content

Commit d0f25e9

Browse files
committed
Add ceil, floor and abs
1 parent 1deab82 commit d0f25e9

2 files changed

Lines changed: 27 additions & 0 deletions

File tree

src/Model/AbstractUnit.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,27 @@ public function round(int $precision = 0, \RoundingMode $mode = \RoundingMode::H
145145
return new static($this->value->round($precision, $mode));
146146
}
147147

148+
public function ceil(): static
149+
{
150+
return new static($this->value->round(0, \RoundingMode::AwayFromZero));
151+
}
152+
153+
public function floor(): static
154+
{
155+
return new static($this->value->round(0, \RoundingMode::TowardsZero));
156+
}
157+
158+
public function abs(): static
159+
{
160+
$value = $this->value->value;
161+
162+
if (str_starts_with($value, '-')) {
163+
$value = substr($value, 1);
164+
}
165+
166+
return new static($value);
167+
}
168+
148169
/**
149170
* @return array<string, class-string<UnitInterface>>
150171
*/

src/Model/UnitInterface.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@ public function format(?int $precision = null, ?\RoundingMode $mode = null): str
2424

2525
public function round(int $precision = 0, \RoundingMode $mode = \RoundingMode::HalfAwayFromZero): static;
2626

27+
public function ceil(): static;
28+
29+
public function floor(): static;
30+
31+
public function abs(): static;
32+
2733
public function convert(string $targetUnitClassOrType): self;
2834

2935
public function normalize(): self;

0 commit comments

Comments
 (0)