Search by

phpmath / bignumber

waltertamboer

A PHP library to work with big numbers.

Package info

github.com/phpmath/bignumber

pkg:composer/phpmath/bignumber

Statistics

Installs: 97 493

Dependents: 1

Suggesters: 0

Stars: 5

Open Issues: 0

2.0.0 2026-11-23 11:30 UTC

This package is auto-updated.

Last update: 2026-11-24 12:59:27 UTC


README

Latest Version on Packagist Software License CI Total Downloads

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 bcmath extension

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() and abs().
  • Operands can be strings, integers, floats (also exponential notation) or other BigNumber instances.
  • Specific exceptions, all implementing BigNumberException.
  • Utils::convertBase() and Utils::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.