MathsHelper   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 27
c 2
b 0
f 0
dl 0
loc 86
rs 10
wmc 12

3 Methods

Rating   Name   Duplication   Size   Complexity  
A round_down() 0 3 1
B round() 0 32 10
A round_up() 0 3 1
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
The private property $default_round is not used, and could be removed.
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