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