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