The integer size in PHP is platform-dependent. The maximum size is usually 2 billion, and the maximum size on 64-bit platforms is usually 9E18.
Floating-point Numbers have limited precision and depend on the system, so never trust floating-point Numbers to be accurate to the last bit, and never compare two floating-point Numbers to be equal.
When the business scenario needs to deal with a large range of values or needs to accurately deal with floating point values, arbitrary precision mathematical functions should be used, such as: trading system, e-commerce system, etc.
The current project encapsulates arbitrary precision mathematical functions to make it easier to solve large number and floating point precision problems in PHP.
First please make sure your PHP has been installed and the BC Math extension, if not support, specific installation reference website: http://php.net/manual/en/bc.installation.php
See the way:
php -info | grep bcmath
If you can see the output
bcmath
bcmath.scale => 0 => 0
BC Math is available
Start the installation:
composer require chawuciren/bignumber
Download the source code directly, introducing src/bignumber.php
Initialization of the incoming numeric string should be used, such as a out of numerical and later returned to the front interface, the type of stored in the database as a DECIMAL
, should first initialize the value of the BigNumber
will be removed, and then used in the code BigNumber
calculated, after the return to use on interface: value () method for numerical output string
use \chawuciren\BigNumber;
$number = new BigNumber('0.002', 3);
use \chawuciren\BigNumber;
$number = BigNumber::build('0.002', 3);
use \chawuciren\BigNumber;
$number = new BigNumber();
$number->valueOf('0.002', 3);
use \chawuciren\BigNumber;
$number = new BigNumber('1.0001', 4);
$number->add('0.0004')->sub('1')->mul('4')->div('5');
var_dump($number->value()); //string(5) "0.0002"
$number2 = new BigNumber('0.0002');
var_dump($number->eq($number2)) //bool true
Set a value to the BigNumber instance
Parameter names | Type | Instructions |
---|---|---|
number | String/BigNumber | A string or number of type BigNumber |
scale | Int | Precision of number |
$number = new \chawuciren\BigNumber();
$number->valueOf('0.002', 3);
var_dump($number); //object(chawuciren\BigNumber)
Returns a value as a string
No parameters
$number = new \chawuciren\BigNumber('0.002', 3);
$str = $number->toString();
var_dump($str); //string(5) "0.002"
Returns a value of type string, currently an alias of the toString method
$number = new \chawuciren\BigNumber('0.002', 3);
$str = $number->value();
var_dump($str); //string(5) "0.002"
Adds the current value plus the number value passed in
Parameter names | Type | Instructions |
---|---|---|
number | String/BigNumber | The value used to add |
$number = new \chawuciren\BigNumber('0.002', 3);
$number->add('0.003');
var_dump($number->value()); //string(5) "0.005"
Subtracts the current value from the number value passed in
Parameter names | Type | Instructions |
---|---|---|
number | String/BigNumber | The value used to subtract |
$number = new \chawuciren\BigNumber('0.002', 3);
$number->sub('0.001');
var_dump($number->value()); //string(5) "0.001"
Multiply the current value by the number value passed in
Parameter names | Type | Instructions |
---|---|---|
number | String/BigNumber | The number used to multiply |
$number = new \chawuciren\BigNumber('0.002', 3);
$number->sub('0.001');
var_dump($number->value()); //string(5) "0.001"
Divide the current value by the number value passed in
Parameter names | Type | Instructions |
---|---|---|
number | String/BigNumber | Divide the current value by the number value passed in |
$number = new \chawuciren\BigNumber('0.002', 3);
$number->div('2');
var_dump($number->value()); //string(5) "0.001"
Modulates the current value with the number value passed in
Parameter names | Type | Instructions |
---|---|---|
number | String/BigNumber | The value used to take a modulus |
$number = new \chawuciren\BigNumber('108');
$number->mod('10');
var_dump($number->value()); //string(1) "8"
Take the current value to the number power
Parameter names | Type | Instructions |
---|---|---|
number | String/BigNumber | The number of powers |
$number = new \chawuciren\BigNumber('2');
$number->pow('2');
var_dump($number->value()); //string(1) "4"
Take the square root of the current value
No parameters
$number = new \chawuciren\BigNumber('16');
$number->sqrt();
var_dump($number->value()); //string(1) "4"
Determine whether the current value equals the value of number
Parameter names | Type | Instructions |
---|---|---|
number | String/BigNumber | The rvalue participating in the judgment |
$number = new \chawuciren\BigNumber('0.00000000000000000001', 20);
$number2 = new \chawuciren\BigNumber('0.00000000000000000001', 20);
var_dump($number->eq($number2)); //bool(true)
Determine whether the current value is greater than the number value
Parameter names | Type | Instructions |
---|---|---|
number | String/BigNumber | The rvalue participating in the judgment |
$number = new \chawuciren\BigNumber('0.00000000000000000002', 20);
$number2 = new \chawuciren\BigNumber('0.00000000000000000001', 20);
var_dump($number->gt($number2)); //bool(true)
Determine whether the current value is greater than or equal to the number value
Parameter names | Type | Instructions |
---|---|---|
number | String/BigNumber | The rvalue participating in the judgment |
$number = new \chawuciren\BigNumber('0.00000000000000000002', 20);
$number2 = new \chawuciren\BigNumber('0.00000000000000000001', 20);
var_dump($number->egt($number2)); //bool(true)
Determine whether the current value is less than the number value
Parameter names | Type | Instructions |
---|---|---|
number | String/BigNumber | The rvalue participating in the judgment |
$number = new \chawuciren\BigNumber('0.00000000000000000002', 20);
$number2 = new \chawuciren\BigNumber('0.00000000000000000001', 20);
var_dump($number->lt($number2)); //bool(false)
Determine whether the current value is less than or equal to the value of number
Parameter names | Type | Instructions |
---|---|---|
number | String/BigNumber | The rvalue participating in the judgment |
$number = new \chawuciren\BigNumber('0.00000000000000000002', 20);
$number2 = new \chawuciren\BigNumber('0.00000000000000000001', 20);
var_dump($number->lt($number2)); //bool(false)