Skip to content
phpmathPublic

About

Immutable arbitrary-precision decimal numbers for PHP, built on bcmath.

Topics

Resources

Contributing

Security policy

Stars

5 stars

Watchers

2 watching

Forks

Repository files navigation

bignumber

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.

About

Immutable arbitrary-precision decimal numbers for PHP, built on bcmath.

Topics

Resources

Contributing

Security policy

Stars

5 stars

Watchers

2 watching

Forks

Releases

Packages

Contributors

Languages