https://github.com/chawuciren/php-bignumber
Using a more modern and concise, object-oriented approach, it is more convenient and intuitive to solve the super large number and floating point precision problems in PHP
https://github.com/chawuciren/php-bignumber
bignumber decimal float number php
Last synced: 10 months ago
JSON representation
Using a more modern and concise, object-oriented approach, it is more convenient and intuitive to solve the super large number and floating point precision problems in PHP
- Host: GitHub
- URL: https://github.com/chawuciren/php-bignumber
- Owner: chawuciren
- Created: 2018-11-12T03:01:06.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2019-06-28T03:04:57.000Z (over 6 years ago)
- Last Synced: 2025-03-23T19:49:15.898Z (11 months ago)
- Topics: bignumber, decimal, float, number, php
- Language: PHP
- Homepage:
- Size: 17.6 KB
- Stars: 19
- Watchers: 1
- Forks: 3
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
### php-bignumber
  
## About
[中文文档](./README_zh.md)
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.
## Installation
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:
#### 1. Way 1: By composer
composer require chawuciren/bignumber
#### 2. Way 2: Directly download and include
Download the source code directly, introducing src/bignumber.php
## Begin to use
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
#### 1. Way 1: use new statements
use \chawuciren\BigNumber;
$number = new BigNumber('0.002', 3);
#### 2. Way 2: use the static method build
use \chawuciren\BigNumber;
$number = BigNumber::build('0.002', 3);
#### 3. Way 3: assign values using the valueOf method
use \chawuciren\BigNumber;
$number = new BigNumber();
$number->valueOf('0.002', 3);
## Sample
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
## Methods list
#### 1.valueOf
Set a value to the BigNumber instance
##### Parameters:
| Parameter names | Type | Instructions |
|--|--|--|
| number | String/BigNumber | A string or number of type BigNumber |
| scale| Int | Precision of number |
##### Return value: BigNumber(Current instance)
##### Sample:
$number = new \chawuciren\BigNumber();
$number->valueOf('0.002', 3);
var_dump($number); //object(chawuciren\BigNumber)
#### 2.toString
Returns a value as a string
##### Parameters:
No parameters
##### Reture value: String(Current value)
##### Sample:
$number = new \chawuciren\BigNumber('0.002', 3);
$str = $number->toString();
var_dump($str); //string(5) "0.002"
#### 3.value
Returns a value of type string, currently an alias of the toString method
##### Sample:
$number = new \chawuciren\BigNumber('0.002', 3);
$str = $number->value();
var_dump($str); //string(5) "0.002"
#### 4.add
Adds the current value plus the number value passed in
##### Parameters:
| Parameter names | Type | Instructions |
|--|--|--|
| number | String/BigNumber | The value used to add |
##### Reture value: BigNumber(Current instance)
##### Sample:
$number = new \chawuciren\BigNumber('0.002', 3);
$number->add('0.003');
var_dump($number->value()); //string(5) "0.005"
#### 5.sub
Subtracts the current value from the number value passed in
##### Parameters:
| Parameter names | Type | Instructions |
|--|--|--|
| number | String/BigNumber | The value used to subtract |
##### Reture value: BigNumber(Current instance)
##### Sample:
$number = new \chawuciren\BigNumber('0.002', 3);
$number->sub('0.001');
var_dump($number->value()); //string(5) "0.001"
#### 6.mul
Multiply the current value by the number value passed in
##### Parameters:
| Parameter names | Type | Instructions |
|--|--|--|
| number | String/BigNumber | The number used to multiply |
##### Reture value: BigNumber(Current instance)
##### Sample:
$number = new \chawuciren\BigNumber('0.002', 3);
$number->sub('0.001');
var_dump($number->value()); //string(5) "0.001"
#### 7.div
Divide the current value by the number value passed in
##### Parameters:
| Parameter names | Type | Instructions |
|--|--|--|
| number | String/BigNumber | Divide the current value by the number value passed in |
##### Reture value: BigNumber(Current instance)
##### Sample:
$number = new \chawuciren\BigNumber('0.002', 3);
$number->div('2');
var_dump($number->value()); //string(5) "0.001"
#### 8.mod
Modulates the current value with the number value passed in
##### Parameters:
| Parameter names | Type | Instructions |
|--|--|--|
| number | String/BigNumber | The value used to take a modulus |
##### Reture value: BigNumber(Current instance)
##### Sample:
$number = new \chawuciren\BigNumber('108');
$number->mod('10');
var_dump($number->value()); //string(1) "8"
#### 9.pow
Take the current value to the number power
##### Parameters:
| Parameter names | Type | Instructions |
|--|--|--|
| number | String/BigNumber | The number of powers |
##### Reture value: BigNumber(Current instance)
##### Sample:
$number = new \chawuciren\BigNumber('2');
$number->pow('2');
var_dump($number->value()); //string(1) "4"
#### 10.sqrt
Take the square root of the current value
##### Parameters:
No parameters
##### Reture value: BigNumber(Current instance)
##### Sample:
$number = new \chawuciren\BigNumber('16');
$number->sqrt();
var_dump($number->value()); //string(1) "4"
#### 11.eq
Determine whether the current value equals the value of number
##### Parameters:
| Parameter names | Type | Instructions |
|--|--|--|
| number | String/BigNumber | The rvalue participating in the judgment |
##### Reture value: Bool (True: equal; False: no equal)
##### Sample:
$number = new \chawuciren\BigNumber('0.00000000000000000001', 20);
$number2 = new \chawuciren\BigNumber('0.00000000000000000001', 20);
var_dump($number->eq($number2)); //bool(true)
#### 12.gt
Determine whether the current value is greater than the number value
##### Parameters:
| Parameter names | Type | Instructions |
|--|--|--|
| number | String/BigNumber | The rvalue participating in the judgment |
##### Reture value: Bool (True: greater than; False: no more than)
##### Sample:
$number = new \chawuciren\BigNumber('0.00000000000000000002', 20);
$number2 = new \chawuciren\BigNumber('0.00000000000000000001', 20);
var_dump($number->gt($number2)); //bool(true)
#### 13.egt
Determine whether the current value is greater than or equal to the number value
##### Parameters:
| Parameter names | Type | Instructions |
|--|--|--|
| number | String/BigNumber | The rvalue participating in the judgment |
##### Reture value: Bool (True: greater than or equal to; False: not greater than and not equal to)
##### Sample:
$number = new \chawuciren\BigNumber('0.00000000000000000002', 20);
$number2 = new \chawuciren\BigNumber('0.00000000000000000001', 20);
var_dump($number->egt($number2)); //bool(true)
#### 14.lt
Determine whether the current value is less than the number value
##### Parameters:
| Parameter names | Type | Instructions |
|--|--|--|
| number | String/BigNumber | The rvalue participating in the judgment |
##### Reture value: Bool (True: less than; False: no less than)
##### Sample:
$number = new \chawuciren\BigNumber('0.00000000000000000002', 20);
$number2 = new \chawuciren\BigNumber('0.00000000000000000001', 20);
var_dump($number->lt($number2)); //bool(false)
#### 15.elt
Determine whether the current value is less than or equal to the value of number
##### Parameters:
| Parameter names | Type | Instructions |
|--|--|--|
| number | String/BigNumber | The rvalue participating in the judgment |
##### Reture value: Bool (True: less than or equal to; False: not less than and not equal to)
##### Sample:
$number = new \chawuciren\BigNumber('0.00000000000000000002', 20);
$number2 = new \chawuciren\BigNumber('0.00000000000000000001', 20);
var_dump($number->lt($number2)); //bool(false)