| @@ 10-33 (lines=24) @@ | ||
| 7 | /** |
|
| 8 | * Round to nearest and break ties by rounding half-way values to nearest even value |
|
| 9 | */ |
|
| 10 | class RoundHalfToEven implements \byrokrat\amount\Rounder |
|
| 11 | { |
|
| 12 | use ToolkitConsumer; |
|
| 13 | ||
| 14 | public function round(string $value, int $precision): string |
|
| 15 | { |
|
| 16 | return $this->toolkit->roundToNearest($value, $precision, function ($value, $precision) { |
|
| 17 | $up = $this->toolkit->roundUp($value, $precision); |
|
| 18 | ||
| 19 | if ($this->toolkit->isEven(substr($up, -1))) { |
|
| 20 | return $up; |
|
| 21 | } |
|
| 22 | ||
| 23 | return $this->toolkit->roundDown($value, $precision); |
|
| 24 | }); |
|
| 25 | } |
|
| 26 | } |
|
| 27 | ||
| @@ 10-33 (lines=24) @@ | ||
| 7 | /** |
|
| 8 | * Round to nearest and break ties by rounding half-way values to nearest odd value |
|
| 9 | */ |
|
| 10 | class RoundHalfToOdd implements \byrokrat\amount\Rounder |
|
| 11 | { |
|
| 12 | use ToolkitConsumer; |
|
| 13 | ||
| 14 | public function round(string $value, int $precision): string |
|
| 15 | { |
|
| 16 | return $this->toolkit->roundToNearest($value, $precision, function ($value, $precision) { |
|
| 17 | $up = $this->toolkit->roundUp($value, $precision); |
|
| 18 | ||
| 19 | if (!$this->toolkit->isEven(substr($up, -1))) { |
|
| 20 | return $up; |
|
| 21 | } |
|
| 22 | ||
| 23 | return $this->toolkit->roundDown($value, $precision); |
|
| 24 | }); |
|
| 25 | } |
|
| 26 | } |
|
| 27 | ||