1
|
|
|
<?php |
2
|
|
|
namespace FilmTools\MRounder; |
3
|
|
|
|
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Returns the given number rounded to the given multiple. |
7
|
|
|
* |
8
|
|
|
* PHP implementation by Nasser Hekmati on StackOverflow |
9
|
|
|
* @see https://stackoverflow.com/a/48643210/3143771 |
10
|
|
|
* @see https://support.office.com/en-us/article/mround-function-c299c3b0-15a5-426d-aa4b-d2d5b3baf427 |
11
|
|
|
* |
12
|
|
|
* @param int|float $number |
13
|
|
|
* @param int|float $multiple |
14
|
|
|
*/ |
15
|
|
View Code Duplication |
function mround( $number, $multiple) |
|
|
|
|
16
|
|
|
{ |
17
|
72 |
|
if (!is_numeric($number)) |
18
|
18 |
|
throw new MRoundInvalidArgumentException("First parameter must be numeric."); |
19
|
|
|
|
20
|
72 |
|
if (!is_numeric($multiple)) |
21
|
18 |
|
throw new MRoundInvalidArgumentException("Second parameter 'multiple' must be numeric."); |
22
|
72 |
|
elseif ( $multiple == 0) |
23
|
|
|
return 0; |
24
|
|
|
|
25
|
72 |
|
return round( $number/$multiple, 0 ) * $multiple; |
26
|
|
|
|
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
// This snippet is not required any longer since we're in namespace here. |
30
|
|
|
# if (!function_exists("mround")): |
31
|
|
|
# endif; |
32
|
|
|
|
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Returns the given number rounded up to the given multiple. |
36
|
|
|
* |
37
|
|
|
* This function corresponds to CEILING function known from Microsoft Excel in LibreOffice/OpenOffice. |
38
|
|
|
* |
39
|
|
|
* @see https://support.office.com/en-us/article/ceiling-function-0a5cd7c8-0720-4f0a-bd2c-c943e510899f |
40
|
|
|
* @see https://help.libreoffice.org/Calc/Mathematical_Functions#CEILING |
41
|
|
|
* |
42
|
|
|
* @param int|float $number |
43
|
|
|
* @param int|float $multiple |
44
|
|
|
*/ |
45
|
|
View Code Duplication |
function mceil( $number, $multiple) |
|
|
|
|
46
|
|
|
{ |
47
|
8 |
|
if (!is_numeric($number)) |
48
|
2 |
|
throw new MRoundInvalidArgumentException("First parameter must be numeric."); |
49
|
|
|
|
50
|
8 |
|
if (!is_numeric($multiple)) |
51
|
2 |
|
throw new MRoundInvalidArgumentException("Second parameter 'multiple' must be numeric."); |
52
|
8 |
|
elseif ( $multiple == 0) |
53
|
|
|
return 0; |
54
|
|
|
|
55
|
8 |
|
return ceil( $number/$multiple ) * $multiple; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Returns the given number rounded down to the given multiple. |
61
|
|
|
* |
62
|
|
|
* This function corresponds the the FLOOR function known from Microsoft Excel in LibreOffice/OpenOffice. |
63
|
|
|
* |
64
|
|
|
* @see https://support.office.com/en-us/article/floor-function-14bb497c-24f2-4e04-b327-b0b4de5a8886 |
65
|
|
|
* @see https://help.libreoffice.org/Calc/Mathematical_Functions#FLOOR |
66
|
|
|
* |
67
|
|
|
* @param int|float $number |
68
|
|
|
* @param int|float $multiple |
69
|
|
|
*/ |
70
|
|
View Code Duplication |
function mfloor( $number, $multiple) |
|
|
|
|
71
|
|
|
{ |
72
|
8 |
|
if (!is_numeric($number)) |
73
|
2 |
|
throw new MRoundInvalidArgumentException("First parameter must be numeric."); |
74
|
|
|
|
75
|
8 |
|
if (!is_numeric($multiple)) |
76
|
2 |
|
throw new MRoundInvalidArgumentException("Second parameter 'multiple' must be numeric."); |
77
|
8 |
|
elseif ( $multiple == 0) |
78
|
|
|
return 0; |
79
|
|
|
|
80
|
8 |
|
return floor( $number/$multiple ) * $multiple; |
81
|
|
|
} |
82
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.