forked from M1ke/easy-site-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
math.php
132 lines (114 loc) · 2.09 KB
/
math.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
<?php
function decimal_ratio($decimal, $sep = ':'){
$num=array(0,0);
do {
$num[1]++;
$num[0] = $num[1]*$decimal;
}
while (!is_whole($num[0]));
$ratio = implode($sep, $num);
return $ratio;
}
function is_decimal($num){
return ((int)$num!=$num);
}
function is_even($n){
return ($n%2 == 0);
}
function is_number(&$number, $blank = null){
if (@strlen($number)>0){
$number = trim($number);
if (is_numeric($number)){
return true;
}
else {
$result=word_to_number($number);
}
}
if (!empty($result) || $blank){
$number = false;
return true;
}
else {
return false;
}
}
/**
* @param $number
* @param null $blank
* @return bool
*
* @deprecated use is_pos which does not pass by reference
*/
function is_positive(&$number, $blank = null){
if (@strlen($number)>0){
$number=trim($number);
if ($number>0 && is_numeric($number)){
return true;
}
else {
return word_to_number($number);
}
}
elseif ($blank){
$number = false;
return true;
}
return false;
}
function is_pos($number){
if (!is_numeric($number)){
return false;
}
return ($number>0);
}
function is_pos_nullable($number){
if (is_null($number)){
return true;
}
return is_pos($number);
}
function is_pos_blank($number){
if ($number===''){
return true;
}
return is_pos_nullable($number);
}
function is_not_zero($number){
$number = abs($number);
return is_pos($number);
}
function is_whole($num){
return $num/round($num)==1;
}
function num_position($num){
$string = (string)$num;
$end = substr($string, strlen($string)-1);
switch ($end){
case '1':
return $num.'st';
break;
case '2':
return $num.'nd';
break;
case '3':
return $num.'rd';
break;
default:
return $num.'th';
}
}
function ratio_decimal($ratio, $sep = ':'){
$num = explode($sep, $ratio);
return $num[1]==0 ? 1 : $num[0]/$num[1];
}
function round_custom($num, $precision = 5, $func = 'round'){
return $precision * $func($num / $precision);
}
// These just seem lazy, should likely check for use and remove them
function round_down($num){
return floor($num);
}
function round_up($num){
return ceil($num);
}