A PHP library to work with big numbers. This library makes use of the BCMath extension to do its calculations.
Every BigNumber is an immutable value object: operations never change the instance, they return a new one.
The full documentation is at https://phpmath.github.io/bignumber/. Every change is listed in the changelog and every release on the releases page.
composer require phpmath/bignumber- PHP 8.4 or newer
- The
bcmathextension
use PHPMath\BigNumber\BigNumber;
$a = new BigNumber('12345678901234567890.5', 2);
$b = new BigNumber('0.25');
$sum = $a->add($b);
echo $sum->getValue(); // 12345678901234567890.75
echo $a; // 12345678901234567890.50 (unchanged)The scale (number of decimals, default 10) is set on construction. Results are truncated, not rounded, to the scale (use round() to round)
of the number the operation is called on. Use withScale() to get a copy with another scale.
add(),subtract(),multiply(),divide(),mod(),pow(),sqrt()andabs().- Operands can be strings, integers, floats (also exponential notation) or other
BigNumberinstances. - Specific exceptions, all implementing
BigNumberException. Utils::convertBase()andUtils::convertToBase10()convert integers of any size between bases 2 and 36.
composer test
composer test-coverage
composer analyse
composer check-style
composer fix-styleAll contributions are welcome. Open an issue or a pull request on GitHub. Please follow the coding standard and add tests.
If you discover a security issue, please create an issue in the issue tracker.
The MIT License (MIT). Please see License File for more information.