phpmath / bignumber
A PHP library to work with big numbers.
Requires
- php: ^8.4
- ext-bcmath: *
Requires (Dev)
- phpmath/coding-standard: ^2.0
- phpstan/phpstan: ^2.3
- phpunit/phpunit: ^13.4
Suggests
None
Provides
None
Conflicts
None
Replaces
None
This package is auto-updated.
Last update: 2026-11-24 12:59:27 UTC
README
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.
Documentation
The full documentation is at https://phpmath.github.io/bignumber/. Every change is listed in the changelog and every release on the releases page.
Install
composer require phpmath/bignumber
Requirements
- PHP 8.4 or newer
- The
bcmathextension
Usage
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.
Features
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.
Development
composer test
composer test-coverage
composer analyse
composer check-style
composer fix-style
Contributing
All contributions are welcome. Open an issue or a pull request on GitHub. Please follow the coding standard and add tests.
Security
If you discover a security issue, please create an issue in the issue tracker.
Credits
License
The MIT License (MIT). Please see License File for more information.