-
Notifications
You must be signed in to change notification settings - Fork 1
/
Numeric.php
46 lines (41 loc) · 1.38 KB
/
Numeric.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
<?php
namespace core;
class Numeric
{
/**
* Calculate amount of decimals we want to show.
* You can forward this function result to the round/number_format funcs
*
* i.e. 5000.5555 = 5000.56
* 0.000055555 = 0.000056
*/
public static function calc_decimals($val) {
$val = strval($val);
$first_zero = $val[0] === "0";
if (! $first_zero) {
// 3. 30. 300. 3000. etc
return 2;
}
$dot = false;
for ($i = 0; $i < strlen($val); $i++) {
$c = $val[$i];
if ($c === ".") {
$dot = true;
continue;
}
$is_nonzero = $c !== "0";
if ($dot && $is_nonzero) {
break;
}
}
// 0. 0.0 0.00 0.000
return $i;
}
/** like ceil() only with the possibility to set significance
* https://www.php.net/manual/en/function.ceil.php#85430
**/
public static function ceiling($number, $significance = 1)
{
return (is_numeric($number) && is_numeric($significance)) ? (ceil($number/$significance)*$significance) : false;
}
}