forked from krissss/bcmath
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBCS.php
More file actions
145 lines (133 loc) · 3.33 KB
/
BCS.php
File metadata and controls
145 lines (133 loc) · 3.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
<?php
namespace kriss\bcmath;
/**
* @method BCS add(...$numbers)
* @method BCS sub(...$numbers)
* @method BCS mul(...$numbers)
* @method BCS div(...$numbers)
* @method BCS mod(...$numbers)
* @method BCS pow(...$numbers) 只支持指数为整数的,若传入小数会自动 intval
*/
class BCS extends BaseBC
{
/**
* 允许使用的bc函数
* @var array
*/
public $bcEnables = ['bcadd', 'bcsub', 'bcmul', 'bcdiv', 'bcmod', 'bcpow'];
/**
* @var string|float
*/
public $result;
/**
* {@inheritdoc}
*/
public function __construct($number, $config = [])
{
parent::__construct($config);
$this->result = $this->numberFormat($number);
}
/**
* @param $number
* @param $config
* @return BCS
*/
public static function create($number, $config = [])
{
return new static($number, $config);
}
public function __call($name, $arguments)
{
$bcName = 'bc' . $name;
if (!in_array($bcName, $this->bcEnables)) {
throw new \Exception("{$bcName} not in ::bcEnables");
}
foreach ($arguments as $number) {
$number = $this->numberFormat($number);
if ($bcName === 'bcpow') {
$number = intval($number);
}
if ($bcName === 'bcmod') {
$this->result = call_user_func($bcName, $this->result, $number);
} else {
$this->result = call_user_func($bcName, $this->result, $number, $this->config['operateScale']);
}
}
return $this;
}
/**
* 获取结果
* @return float|string
*/
public function getResult()
{
return $this->getScaleNumber($this->result);
}
/**
* 获取平方根
* @return string
*/
public function getSqrt()
{
return bcsqrt($this->result, $this->config['scale']);
}
/**
* 比较结果
* @param $number
* @return int
*/
public function compare($number)
{
return bccomp($this->result, $this->numberFormat($number), $this->config['scale']);
}
/**
* 是否相等
* @param $number
* @return bool
*/
public function isEqual($number)
{
return $this->compare($number) === 0;
}
/**
* 是否小于
* @param $number
* @return bool
*/
public function isLessThan($number)
{
return $this->compare($number) === -1;
}
/**
* 是否大于
* @param $number
* @return bool
*/
public function isLargerThan($number)
{
return $this->compare($number) === 1;
}
/**
* 格式化数字
* @param $number
* @return string|float|int
*/
private function numberFormat($number)
{
if (is_null($number)) {
// null直接返回 0
return '0';
}
if (is_float($number)) {
// 将 float 转为 string,科学计数法可以正常变成 8.0E-6
$number = (string)$number;
}
if (
is_string($number)
&& (strpos($number, 'E') !== false || strpos($number, 'e') !== false) // 科学计数法
) {
return number_format($number, $this->config['operateScaleNumberFormat'], '.', '');
}
return $number;
}
}