Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
DeltaPriceRound.php
Go to the documentation of this file.
1 <?php
6 declare(strict_types=1);
7 
9 
11 
16 {
20  private $priceCurrency;
21 
25  private $roundingDeltas;
26 
30  public function __construct(PriceCurrencyInterface $priceCurrency)
31  {
32  $this->priceCurrency = $priceCurrency;
33  }
34 
42  public function round(float $price, string $type): float
43  {
44  if ($price) {
45  // initialize the delta to a small number to avoid non-deterministic behavior with rounding of 0.5
46  $delta = isset($this->roundingDeltas[$type]) ? $this->roundingDeltas[$type] : 0.000001;
47  $price += $delta;
48  $roundPrice = $this->priceCurrency->round($price);
49  $this->roundingDeltas[$type] = $price - $roundPrice;
50  $price = $roundPrice;
51  }
52 
53  return $price;
54  }
55 
61  public function resetAll(): void
62  {
63  $this->roundingDeltas = [];
64  }
65 
72  public function reset(string $type): void
73  {
74  if (isset($this->roundingDeltas[$type])) {
75  unset($this->roundingDeltas[$type]);
76  }
77  }
78 }
$price
$type
Definition: item.phtml:13
__construct(PriceCurrencyInterface $priceCurrency)
round(float $price, string $type)