Calculator.php 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Math;
  7. /**
  8. * Calculations Library
  9. *
  10. * @api
  11. * @since 100.0.2
  12. */
  13. class Calculator
  14. {
  15. /**
  16. * Delta collected during rounding steps
  17. *
  18. * @var float
  19. */
  20. protected $_delta = 0.0;
  21. /**
  22. * @var \Magento\Framework\Pricing\PriceCurrencyInterface|null
  23. */
  24. protected $priceCurrency;
  25. /**
  26. * @param \Magento\Framework\Pricing\PriceCurrencyInterface $priceCurrency
  27. */
  28. public function __construct(\Magento\Framework\Pricing\PriceCurrencyInterface $priceCurrency)
  29. {
  30. $this->priceCurrency = $priceCurrency;
  31. }
  32. /**
  33. * Round price considering delta
  34. *
  35. * @param float $price
  36. * @param bool $negative Indicates if we perform addition (true) or subtraction (false) of rounded value
  37. * @return float
  38. */
  39. public function deltaRound($price, $negative = false)
  40. {
  41. $roundedPrice = $price;
  42. if ($roundedPrice) {
  43. if ($negative) {
  44. $this->_delta = -$this->_delta;
  45. }
  46. $price += $this->_delta;
  47. $roundedPrice = $this->priceCurrency->round($price);
  48. $this->_delta = $price - $roundedPrice;
  49. if ($negative) {
  50. $this->_delta = -$this->_delta;
  51. }
  52. }
  53. return $roundedPrice;
  54. }
  55. }