|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Bavix\Wallet\Simple; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Class BCMath. |
|
7
|
|
|
* @deprecated Will be removed in 6.x. |
|
8
|
|
|
*/ |
|
9
|
|
|
class BCMath extends Math |
|
|
|
|
|
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* {@inheritdoc} |
|
13
|
|
|
*/ |
|
14
|
115 |
|
public function add($first, $second, ?int $scale = null): string |
|
15
|
|
|
{ |
|
16
|
115 |
|
return bcadd($first, $second, $this->scale($scale)); |
|
17
|
|
|
} |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* {@inheritdoc} |
|
21
|
|
|
*/ |
|
22
|
52 |
|
public function sub($first, $second, ?int $scale = null): string |
|
23
|
|
|
{ |
|
24
|
52 |
|
return bcsub($first, $second, $this->scale($scale)); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* {@inheritdoc} |
|
29
|
|
|
*/ |
|
30
|
32 |
|
public function div($first, $second, ?int $scale = null): string |
|
31
|
|
|
{ |
|
32
|
32 |
|
return bcdiv($first, $second, $this->scale($scale)); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* {@inheritdoc} |
|
37
|
|
|
*/ |
|
38
|
34 |
|
public function mul($first, $second, ?int $scale = null): string |
|
39
|
|
|
{ |
|
40
|
34 |
|
return bcmul($first, $second, $this->scale($scale)); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* {@inheritdoc} |
|
45
|
|
|
*/ |
|
46
|
18 |
|
public function pow($first, $second, ?int $scale = null): string |
|
47
|
|
|
{ |
|
48
|
18 |
|
return bcpow($first, $second, $this->scale($scale)); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* {@inheritdoc} |
|
53
|
|
|
*/ |
|
54
|
1 |
|
public function ceil($number): string |
|
55
|
|
|
{ |
|
56
|
1 |
|
if (strpos($number, '.') === false) { |
|
57
|
1 |
|
return $number; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
1 |
|
if (preg_match("~\.[0]+$~", $number)) { |
|
61
|
1 |
|
return $this->round($number, 0); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
1 |
|
if ($this->isNegative($number)) { |
|
65
|
1 |
|
return bcsub($number, 0, 0); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
1 |
|
return bcadd($number, 1, 0); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* {@inheritdoc} |
|
73
|
|
|
*/ |
|
74
|
16 |
|
public function floor($number): string |
|
75
|
|
|
{ |
|
76
|
16 |
|
if (strpos($number, '.') === false) { |
|
77
|
1 |
|
return $number; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
16 |
|
if (preg_match("~\.[0]+$~", $number)) { |
|
81
|
4 |
|
return $this->round($number, 0); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
13 |
|
if ($this->isNegative($number)) { |
|
85
|
1 |
|
return bcsub($number, 1, 0); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
13 |
|
return bcadd($number, 0, 0); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* {@inheritdoc} |
|
93
|
|
|
*/ |
|
94
|
129 |
|
public function round($number, int $precision = 0): string |
|
95
|
|
|
{ |
|
96
|
129 |
|
if (strpos($number, '.') === false) { |
|
97
|
126 |
|
return $number; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
120 |
|
if ($this->isNegative($number)) { |
|
101
|
29 |
|
return bcsub($number, '0.'.str_repeat('0', $precision).'5', $precision); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
114 |
|
return bcadd($number, '0.'.str_repeat('0', $precision).'5', $precision); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* @param float|int|string $number |
|
109
|
|
|
* @return string |
|
110
|
|
|
*/ |
|
111
|
53 |
|
public function abs($number): string |
|
112
|
|
|
{ |
|
113
|
53 |
|
if (! preg_match('~^-?\d*(\.\d*)?$~', $number, $matches)) { |
|
114
|
1 |
|
return 0; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
53 |
|
$digits = $matches[0] ?? '0'; |
|
118
|
53 |
|
$division = $matches[1] ?? '.'; |
|
119
|
53 |
|
if ($digits === '.' && $division === '.') { |
|
120
|
1 |
|
return 0; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
53 |
|
if ($this->isNegative($number)) { |
|
124
|
53 |
|
return substr($number, 1); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
51 |
|
return $number; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* {@inheritdoc} |
|
132
|
|
|
*/ |
|
133
|
119 |
|
public function compare($first, $second): int |
|
134
|
|
|
{ |
|
135
|
119 |
|
return bccomp($first, $second, $this->scale()); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* @param $number |
|
140
|
|
|
* @return bool |
|
141
|
|
|
*/ |
|
142
|
121 |
|
protected function isNegative($number): bool |
|
143
|
|
|
{ |
|
144
|
121 |
|
return strpos($number, '-') === 0; |
|
145
|
|
|
} |
|
146
|
|
|
} |
|
147
|
|
|
|