silvercommerce /
tax-admin
| 1 | <?php |
||
| 2 | |||
| 3 | namespace SilverCommerce\TaxAdmin\Helpers; |
||
| 4 | |||
| 5 | use SilverStripe\Core\Config\Config; |
||
| 6 | use SilverStripe\Core\Config\Configurable; |
||
| 7 | use SilverStripe\Core\Injector\Injectable; |
||
| 8 | |||
| 9 | /** |
||
| 10 | * Simple helper class to provide generic functions to help with |
||
| 11 | * maths functions |
||
| 12 | */ |
||
| 13 | class MathsHelper |
||
| 14 | { |
||
| 15 | use Injectable; |
||
| 16 | use Configurable; |
||
| 17 | |||
| 18 | const ROUND_DEFAULT = 1; |
||
| 19 | |||
| 20 | const ROUND_UP = 2; |
||
| 21 | |||
| 22 | const ROUND_DOWN = 3; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * The default rounding used by this class |
||
| 26 | */ |
||
| 27 | private static $default_round = self::ROUND_DEFAULT; |
||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 28 | |||
| 29 | /** |
||
| 30 | * Rounds up a float to a specified number of decimal places |
||
| 31 | * (basically acts like ceil() but allows for decimal places) |
||
| 32 | * |
||
| 33 | * @param float $value Float to round up |
||
| 34 | * @param int $places the number of decimal places to round to |
||
| 35 | * |
||
| 36 | * @return float |
||
| 37 | */ |
||
| 38 | public static function round_up($value, $places = 0) |
||
| 39 | { |
||
| 40 | return self::round($value, $places, self::ROUND_UP); |
||
| 41 | } |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Rounds up a float to a specified number of decimal places |
||
| 45 | * (basically acts like ceil() but allows for decimal places) |
||
| 46 | * |
||
| 47 | * @param float $value Float to round up |
||
| 48 | * @param int $places the number of decimal places to round to |
||
| 49 | * |
||
| 50 | * @return float |
||
| 51 | */ |
||
| 52 | public static function round_down($value, $places = 0) |
||
| 53 | { |
||
| 54 | return self::round($value, $places, self::ROUND_DOWN); |
||
| 55 | } |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Round the provided value to the defined number of places in the direction |
||
| 59 | * provided (up or down). |
||
| 60 | * |
||
| 61 | * @param float $value The value we want to round |
||
| 62 | * @param int $places The number of decimal places to round to |
||
| 63 | * @param boolean $down Do we round down? If false value will be rounded up |
||
| 64 | * |
||
| 65 | * @return float |
||
| 66 | */ |
||
| 67 | public static function round($value, $places = 0, $type = null) |
||
| 68 | { |
||
| 69 | if (!isset($type)) { |
||
| 70 | $type = Config::inst()->get(self::class, "default_round"); |
||
| 71 | } |
||
| 72 | |||
| 73 | $offset = 0; |
||
| 74 | |||
| 75 | // If we are rounding to decimals get a more granular number. |
||
| 76 | if ($places !== 0 && $type !== self::ROUND_DEFAULT) { |
||
| 77 | if ($type == self::ROUND_DOWN) { |
||
| 78 | $offset = -0.45; |
||
| 79 | } elseif ($type == self::ROUND_UP) { |
||
| 80 | $offset = 0.45; |
||
| 81 | } |
||
| 82 | $offset /= pow(10, $places) + 1; |
||
| 83 | } |
||
| 84 | |||
| 85 | // if we are rounding to whole numbers and forcing up |
||
| 86 | // down, use ceil/floor |
||
| 87 | if ($places == 0 && $type == self::ROUND_UP) { |
||
| 88 | $return = ceil($value); |
||
| 89 | } elseif ($places == 0 && $type == self::ROUND_DOWN) { |
||
| 90 | $return = floor($value); |
||
| 91 | } else { |
||
| 92 | $return = round( |
||
| 93 | $value + $offset, |
||
| 94 | $places |
||
| 95 | ); |
||
| 96 | } |
||
| 97 | |||
| 98 | return $return; |
||
| 99 | } |
||
| 100 | } |
||
| 101 |