Skip to content

Commit

Permalink
Add: default precision & rounding mode constants
Browse files Browse the repository at this point in the history
  • Loading branch information
jordanbrauer committed Feb 4, 2018
1 parent 39ffd1b commit cee784b
Showing 1 changed file with 32 additions and 2 deletions.
34 changes: 32 additions & 2 deletions src/UnitConverter/Calculator/AbstractCalculator.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,36 @@
*/
abstract class AbstractCalculator implements CalculatorInterface
{
/**
* @const int DEFAULT_PRECISION The default precision value.
*/
const DEFAULT_PRECISION = 2;

/**
* @const int DEFAULT_ROUNDING_MODE The default rounding mode for calculators.
*/
const DEFAULT_ROUNDING_MODE = self::ROUND_HALF_UP;

/**
* @const int ROUND_HALF_UP Makes 1.5 into 2 and -1.5 into -2.
*/
const ROUND_HALF_UP = PHP_ROUND_HALF_UP;

/**
* @const int ROUND_HALF_DOWN Makes 1.5 into 1 and -1.5 into -1.
*/
const ROUND_HALF_DOWN = PHP_ROUND_HALF_DOWN;

/**
* @const int ROUND_HALF_EVEN Rounds to the nearest even value.
*/
const ROUND_HALF_EVEN = PHP_ROUND_HALF_EVEN;

/**
* @const int ROUND_HALF_ODD Rounds to the nearest odd value.
*/
const ROUND_HALF_ODD = PHP_ROUND_HALF_ODD;

/**
* @var int $precision The number of decimal places that will calculated
*/
Expand All @@ -45,8 +75,8 @@ abstract class AbstractCalculator implements CalculatorInterface
*/
public function __construct (int $precision = null, int $roundingMode = null)
{
$this->setPrecision(($precision ?? 2));
$this->setRoundingMode(($roundingMode ?? PHP_ROUND_HALF_UP));
$this->setPrecision(($precision ?? self::DEFAULT_PRECISION));
$this->setRoundingMode(($roundingMode ?? self::DEFAULT_ROUNDING_MODE));
}

public function setPrecision (int $precision): CalculatorInterface
Expand Down

0 comments on commit cee784b

Please sign in to comment.