Skip to content

Commit 3f9723a

Browse files
committed
new utility function for numbers
1 parent 518a2bf commit 3f9723a

File tree

1 file changed

+102
-72
lines changed

1 file changed

+102
-72
lines changed

src/Util/NumberUtil.php

Lines changed: 102 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -25,80 +25,110 @@
2525
*
2626
* @package doganoo\PHPUtil\Util
2727
*/
28-
final class NumberUtil{
29-
/**
30-
* prevent from instantiation
31-
* StringUtil constructor.
32-
*/
33-
private function __construct(){
34-
}
28+
final class NumberUtil {
29+
/**
30+
* prevent from instantiation
31+
* StringUtil constructor.
32+
*/
33+
private function __construct() {
34+
}
3535

36-
/**
37-
* returns the formatted number with grouped thousands
38-
*
39-
* @param $value
40-
* @param int $decimals
41-
*
42-
* @return null|string
43-
*/
44-
public static function format($value, int $decimals = 2): ?string{
45-
if(!\is_numeric($value)){
46-
return null;
47-
}
48-
return \number_format($value, $decimals);
49-
}
36+
/**
37+
* returns the formatted number with grouped thousands
38+
*
39+
* @param $value
40+
* @param int $decimals
41+
*
42+
* @return null|string
43+
*/
44+
public static function format($value, int $decimals = 2): ?string {
45+
if (!\is_numeric($value)) {
46+
return null;
47+
}
48+
return \number_format($value, $decimals);
49+
}
5050

51-
/**
52-
* returns an array with the elements
53-
*
54-
* @param int $number
55-
*
56-
* @return array
57-
*/
58-
public static function intToArray(int $number): array{
59-
return StringUtil::stringToArray($number);
60-
}
51+
/**
52+
* returns an array with the elements
53+
*
54+
* @param int $number
55+
*
56+
* @return array
57+
*/
58+
public static function intToArray(int $number): array {
59+
return StringUtil::stringToArray($number);
60+
}
6161

62-
/**
63-
* This method checks if $value is greater than $value1. If $gte is set to
64-
* true, the method checks if $value is greater than or equal to $value1.
65-
* From http://php.net/manual/de/language.types.float.php:
66-
* So never trust floating number results to the last digit, and do not
67-
* compare floating point numbers directly for equality.
68-
* Contributed notes in http://php.net/manual/de/language.types.float.php
69-
* suggests rounding the values before comparing (see 115 catalin dot luntraru at gmail dot com).
70-
*
71-
* @param float $value
72-
* @param float $value1
73-
* @param bool $gte
74-
*
75-
* @return bool
76-
* @since 1.0.0
77-
*/
78-
public static function floatGreaterThan(float $value, float $value1, bool $gte = false){
79-
$value = round($value, 10, PHP_ROUND_HALF_EVEN);
80-
$value1 = round($value1, 10, PHP_ROUND_HALF_EVEN);
81-
if($gte){
82-
return $value >= $value1;
83-
} else{
84-
return $value > $value1;
85-
}
86-
}
62+
/**
63+
* This method checks if $value is greater than $value1. If $gte is set to
64+
* true, the method checks if $value is greater than or equal to $value1.
65+
* From http://php.net/manual/de/language.types.float.php:
66+
* So never trust floating number results to the last digit, and do not
67+
* compare floating point numbers directly for equality.
68+
* Contributed notes in http://php.net/manual/de/language.types.float.php
69+
* suggests rounding the values before comparing (see 115 catalin dot luntraru at gmail dot com).
70+
*
71+
* @param float $value
72+
* @param float $value1
73+
* @param bool $gte
74+
*
75+
* @return bool
76+
* @since 1.0.0
77+
*/
78+
public static function floatGreaterThan(float $value, float $value1, bool $gte = false) {
79+
$value = round($value, 10, PHP_ROUND_HALF_EVEN);
80+
$value1 = round($value1, 10, PHP_ROUND_HALF_EVEN);
81+
if ($gte) {
82+
return $value >= $value1;
83+
} else {
84+
return $value > $value1;
85+
}
86+
}
8787

88-
/**
89-
* This method compares two float numbers for equality. PHP float values
90-
* should never be compared directly, according to: http://php.net/manual/de/language.types.float.php
91-
*
92-
* @param float $value
93-
* @param float $value1
94-
*
95-
* @return bool
96-
*/
97-
public static function compareFloat(float $value, float $value1){
98-
$epsilon = 0.00001;
99-
if(abs($value - $value1) < $epsilon){
100-
return true;
101-
}
102-
return false;
103-
}
88+
/**
89+
* This method compares two float numbers for equality. PHP float values
90+
* should never be compared directly, according to: http://php.net/manual/de/language.types.float.php
91+
*
92+
* @param float $value
93+
* @param float $value1
94+
*
95+
* @return bool
96+
*/
97+
public static function compareFloat(float $value, float $value1) {
98+
$epsilon = 0.00001;
99+
if (abs($value - $value1) < $epsilon) {
100+
return true;
101+
}
102+
return false;
103+
}
104+
105+
/**
106+
* strips all non integer fields from the input and returns all
107+
* values as integer data type.
108+
*
109+
* @param array $array
110+
* @return array
111+
*/
112+
public static function stripNonInteger(array $array): array {
113+
$newArray = [];
114+
foreach ($array as $value) {
115+
if (!NumberUtil::isInteger($value)) continue;
116+
$newArray[] = \intval($value);
117+
}
118+
return $newArray;
119+
}
120+
121+
/**
122+
* whether $value is an integer or not.
123+
* Notice that intval() returns a zero if the value is not an integer :/
124+
*
125+
* Therefore, we use "filter_var()" method, see here:
126+
* https://stackoverflow.com/a/29018655/1966490
127+
*
128+
* @param $value
129+
* @return bool
130+
*/
131+
public static function isInteger($value): bool {
132+
return false !== filter_var($value, FILTER_VALIDATE_INT);
133+
}
104134
}

0 commit comments

Comments
 (0)