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