2
2
3
3
namespace Utilitte \Php \Numbers ;
4
4
5
+ use InvalidArgumentException ;
5
6
use LogicException ;
7
+ use Utilitte \Php \Numbers ;
6
8
7
9
final class NumberFormatter
8
10
{
9
11
10
- public static function formatBytes (int |float $ number , ?int $ decimals = null , bool $ fixed = false ): string
12
+ public static function formatBytes (string | int |float $ number , ?int $ decimals = null , bool $ fixed = true ): string
11
13
{
12
14
static $ end = 'PB ' ;
13
15
static $ units = ['B ' , 'kB ' , 'MB ' , 'GB ' , 'TB ' , 'PB ' ];
14
16
17
+ $ number = Numbers::convertToFloat ($ number );
18
+
15
19
$ unit = 'B ' ;
16
20
foreach ($ units as $ unit ) {
17
21
if (abs ($ number ) < 1024 || $ unit === $ end ) {
@@ -21,10 +25,10 @@ public static function formatBytes(int|float $number, ?int $decimals = null, boo
21
25
$ number /= 1024 ;
22
26
}
23
27
24
- return self ::formatNumber ($ number , $ decimals , $ fixed ). $ unit ;
28
+ return self ::formatNumber ($ number , $ decimals , $ fixed ) . $ unit ;
25
29
}
26
30
27
- public static function formatShort (int |float $ number , ?int $ decimals = null , bool $ fixed = false ): string
31
+ public static function formatShort (string | int |float $ number , ?int $ decimals = null , bool $ fixed = true ): string
28
32
{
29
33
static $ formatters = [
30
34
'' => 1_000 ,
@@ -34,6 +38,7 @@ public static function formatShort(int|float $number, ?int $decimals = null, boo
34
38
'T ' => null ,
35
39
];
36
40
41
+ $ number = Numbers::convertToFloat ($ number );
37
42
$ divider = 1 ;
38
43
foreach ($ formatters as $ str => $ limit ) {
39
44
if ($ limit === null || $ number < $ limit ) {
@@ -47,48 +52,35 @@ public static function formatShort(int|float $number, ?int $decimals = null, boo
47
52
}
48
53
49
54
public static function formatPercentage (
50
- string |int |float |null $ number ,
51
- int $ decimals = 2 ,
52
- bool $ sign = true ,
55
+ string |int |float $ number ,
56
+ ?int $ decimals = null ,
53
57
bool $ fixed = true ,
54
- ): ?string
58
+ bool $ sign = false ,
59
+ ): string
55
60
{
56
- if ($ number === null || is_string ($ number ) && !is_numeric ($ number )) {
57
- return null ;
58
- }
59
-
60
- $ value = self ::formatNumber ($ number , $ decimals , $ fixed );
61
-
62
- if ($ value === null ) {
63
- return null ;
64
- }
65
-
66
- return ($ sign && $ number > 0 ? '+ ' : '' ) . $ value . '% ' ;
61
+ return self ::formatNumber ($ number , $ decimals , $ fixed ) . '% ' ;
67
62
}
68
63
69
- public static function formatNumber (string |int |float |null $ number , ?int $ decimals = null , bool $ fixed = false ): ?string
64
+ public static function formatNumber (
65
+ string |int |float |null $ number ,
66
+ ?int $ decimals = null ,
67
+ bool $ fixed = true ,
68
+ bool $ sign = false ,
69
+ ): string
70
70
{
71
- if (!is_numeric ($ number )) {
72
- return null ;
73
- }
74
-
75
- $ number = (float ) $ number ;
76
-
77
- if (is_nan ($ number ) || is_infinite ($ number )) {
78
- return null ;
79
- }
71
+ $ number = Numbers::convertToFloat ($ number );
80
72
81
73
if ($ decimals === null ) {
82
- $ decimals = 2 ;
83
-
84
- if ($ number < 0.01 ) {
85
- $ decimals = 6 ;
86
- } elseif ($ number < 0.1 ) {
74
+ if ($ fixed === true ) {
75
+ $ decimals = 0 ;
76
+ } elseif ($ number < 0.01 ) {
87
77
$ decimals = 5 ;
88
- } elseif ($ number < 1 ) {
78
+ } elseif ($ number < 0. 1 ) {
89
79
$ decimals = 4 ;
90
- } elseif ($ number < 10 ) {
80
+ } elseif ($ number < 1 ) {
91
81
$ decimals = 3 ;
82
+ } else {
83
+ $ decimals = 2 ;
92
84
}
93
85
}
94
86
@@ -98,7 +90,7 @@ public static function formatNumber(string|int|float|null $number, ?int $decimal
98
90
$ number = self ::removeZerosAfterDot ($ number );
99
91
}
100
92
101
- return $ number ;
93
+ return ( $ sign && $ number > 0 ? ' + ' : '' ) . $ number . ' % ' ;
102
94
}
103
95
104
96
public static function removeZerosAfterDot (string $ number ): string
0 commit comments