1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Jnjxp Rinc |
4
|
|
|
* |
5
|
|
|
* PHP version 5 |
6
|
|
|
* |
7
|
|
|
* Copyright (C) 2016 Jake Johns |
8
|
|
|
* |
9
|
|
|
* This software may be modified and distributed under the terms |
10
|
|
|
* of the MIT license. See the LICENSE file for details. |
11
|
|
|
* |
12
|
|
|
* @category RoundTo |
13
|
|
|
* @package Jnjxp\Rinc |
14
|
|
|
* @author Jake Johns <[email protected]> |
15
|
|
|
* @copyright 2016 Jake Johns |
16
|
|
|
* @license http://jnj.mit-license.org/2016 MIT License |
17
|
|
|
* @link https://github.com/jnjxp/jnjxp.rinc |
18
|
|
|
*/ |
19
|
|
|
|
20
|
|
|
namespace Jnjxp\Rinc; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* RoundTo |
24
|
|
|
* |
25
|
|
|
* @category RoundTo |
26
|
|
|
* @package Jnjxp\Rinc |
27
|
|
|
* @author Jake Johns <[email protected]> |
28
|
|
|
* @license http://jnj.mit-license.org/ MIT License |
29
|
|
|
* @link https://github.com/jnjxp/jnjxp.rinc |
30
|
|
|
*/ |
31
|
|
|
class RoundTo |
32
|
|
|
{ |
33
|
|
|
/** |
34
|
|
|
* Round to nearest increment |
35
|
|
|
* |
36
|
|
|
* @param float $increment incremenet to round to |
37
|
|
|
* @param float $value value to round |
38
|
|
|
* |
39
|
|
|
* @return float |
40
|
|
|
* |
41
|
|
|
* @access public |
42
|
|
|
*/ |
43
|
1 |
|
public function nearest($increment, $value) |
44
|
|
|
{ |
45
|
1 |
|
$value = $value / $increment; |
46
|
1 |
|
$value = round($value, 0); |
47
|
1 |
|
return $value * $increment; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Round to next increment |
52
|
|
|
* |
53
|
|
|
* @param float $increment incremenet to round to |
54
|
|
|
* @param float $value value to round |
55
|
|
|
* |
56
|
|
|
* @return float |
57
|
|
|
* |
58
|
|
|
* @access public |
59
|
|
|
*/ |
60
|
1 |
|
public function next($increment, $value) |
61
|
|
|
{ |
62
|
1 |
|
$value = $value / $increment; |
63
|
1 |
|
$value = ceil($value); |
64
|
1 |
|
return $value * $increment; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Round to previous increment |
69
|
|
|
* |
70
|
|
|
* @param float $increment incremenet to round to |
71
|
|
|
* @param float $value value to round |
72
|
|
|
* |
73
|
|
|
* @return float |
74
|
|
|
* |
75
|
|
|
* @access public |
76
|
|
|
*/ |
77
|
1 |
|
public function previous($increment, $value) |
78
|
|
|
{ |
79
|
1 |
|
$value = $value / $increment; |
80
|
1 |
|
$value = floor($value); |
81
|
1 |
|
return $value * $increment; |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|