PHPackages                             tetthys/bc - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. tetthys/bc

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

tetthys/bc
==========

Bc is a small and simple but accurate tool for calculation.

1.0.0(1y ago)556MITPHPPHP &gt;=8.1

Since Jun 7Pushed 1y ago1 watchersCompare

[ Source](https://github.com/tetthys/Bc)[ Packagist](https://packagist.org/packages/tetthys/bc)[ RSS](/packages/tetthys-bc/feed)WikiDiscussions 1.0.0 Synced 1mo ago

READMEChangelog (3)Dependencies (1)Versions (6)Used By (0)

- [System Requirements](#system-requirements)
- [Bc](#bc)
- [Why Bc?](#why-bc)
- [Usage Examples](#usage-examples)
    - [Calculation](#calculation)
    - [Comparison](#comparison)
- [About `num` method](#about-num-method)
- [About `scale` method](#about-scale-method)
- [Supported Calculation Methods](#supported-calculation-methods)
    - [add](#add)
    - [sub](#sub)
    - [mul](#mul)
    - [div](#div)
    - [mod](#mod)
    - [pow](#pow)
    - [powmod](#powmod)
    - [sqrt](#sqrt)
- [Supported Comparison Methods](#supported-comparison-methods)
    - [isGreaterThan](#isgreaterthan)
    - [isGreaterThanOrEqual](#isgreaterthanorequal)
    - [isLessThan](#islessthan)
    - [isLessThanOrEqual](#islessthanorequal)
    - [isEqual](#isequal)
    - [isDifferent](#isdifferent)
    - [gt](#gt)
    - [gte](#gte)
    - [lt](#lt)
    - [lte](#lte)
    - [is](#is)
    - [isNot](#isnot)
- [Runtime Exceptions](#runtime-exceptions)
    - [ScaleCannotBeUsedForOperation](#scalecannotbeusedforoperation)
    - [ValueCannotBeUsedForOperation](#valuecannotbeusedforoperation)
- [How to contribute and test in same environment?](#how-to-contribute-and-test-in-same-environment)
    - [docker-compose up](#docker-compose-up)
    - [attach shell to phptestenv container](#attach-shell-to-phptestenv-container)
    - [install dependencies with composer](#install-dependencies-with-composer)
    - [run test](#run-test)
- [What I want to say](#what-i-want-to-say)

System Requirements
===================

[](#system-requirements)

- php 8.1 or later
- php bcmath extension

Bc
==

[](#bc)

Bc is a small and simple but accurate tool for calculation. It uses [bcmath](https://www.php.net/manual/en/book.bc.php) functions internally.

You can get the exact result you want.

There are no rounding issues if you have enough memory.

It's like calculating to N decimal places using a pen and paper with your hands.

Why Bc?
=======

[](#why-bc)

Do you think below test will pass?

```
it('shows that 0.1 + 0.2 = 0.3', function () {
    expect(0.1 + 0.2)->toBe(0.3);
});
```

**Unfortunately, 0.1 + 0.2 is not 0.3 in php**

It fails for the following reason:

> Failed asserting that 0.30000000000000004 is identical to 0.3.

That's why I made this.

Usage Examples
==============

[](#usage-examples)

Calculation
-----------

[](#calculation)

After giving a scale value, you can calculate in order.

```
(new Bc)->scale(2)->num('1')->add('2')->mul('3')->value(); // '9.00'
```

It also can be used like below:

```
(new Bc('1'))->add(new Bc('2'))->mul(new Bc('3'))->value(); // '9'
```

```
(new Bc)->scale(2)->num((new Bc('1')))->add(new Bc('2'))->mul(new Bc('3'))->value(); // '9.00'
```

Comparison
----------

[](#comparison)

After giving a scale value, you can compare.

```
// true for '10.00' > '1.00'
(new Bc)->scale(2)->num('10')
    ->isGreaterThan('1');
```

It also can be used like below:

```
// true for '30.00' > '3.00'
(new Bc)->scale(2)->num('10')->add('20')
    ->isGreaterThan((new Bc)->scale(2)->num('1')->add('2'));
```

About `num` method
==================

[](#about-num-method)

It specifies a number at which the calculation begins. It always returns `Bc` instance.

```
(new Bc)->num('1')
```

Or you can use the below instead:

```
(new Bc('1'))
```

About `scale` method
====================

[](#about-scale-method)

It specifies a scale value to be passed on to a next operation. The default value is 0.

```
(new Bc)->num('1')->add('2')->value(); // '3'
```

It supports chaining:

```
// With scale 0, '1' + '2' = '3'
// With scale 2, '3.00' * '3.00' = '9.00'
(new Bc)->num('1')->add('2')->scale(2)->mul('3')->value(); // '9.00'
```

Supported Calculation Methods
=============================

[](#supported-calculation-methods)

Calculation methods expect `string` or `Bc` instance. And they always return `Bc` instance.

add
---

[](#add)

It adds a number

```
(new Bc)->num('1')->add('2')->value(); // '3'
```

sub
---

[](#sub)

It subtracts a number

```
(new Bc)->num('2')->sub('1')->value(); // '1'
```

mul
---

[](#mul)

It multiplies a number

```
(new Bc)->num('2')->mul('3')->value(); // '6'
```

div
---

[](#div)

It divides a number

```
(new Bc)->num('6')->div('3')->value(); // '2'
```

mod
---

[](#mod)

It calculates the modulus of a number

```
(new Bc)->num('10')->mod('7')->value(); // '3'
```

pow
---

[](#pow)

It calculates the power of a number

```
(new Bc)->num('2')->pow('3')->value(); // '8'
```

powmod
------

[](#powmod)

It calculates the power of a number with modulus

```
(new Bc)->num('2')->powmod('5', '3')->value(); // '2'
```

sqrt
----

[](#sqrt)

It calculates the square root of a number

```
(new Bc)->num('9')->sqrt()->value(); // '3'
```

Supported Comparison Methods
============================

[](#supported-comparison-methods)

Comparison methods expect `string` or `Bc` instance. And they always return `bool`.

isGreaterThan
-------------

[](#isgreaterthan)

It checks if a number is greater than another number

```
(new Bc)->num('10')->isGreaterThan('1'); // true
```

isGreaterThanOrEqual
--------------------

[](#isgreaterthanorequal)

It checks if a number is greater than or equal to another number

```
(new Bc)->num('10')->isGreaterThanOrEqual('10'); // true
```

isLessThan
----------

[](#islessthan)

It checks if a number is less than another number

```
(new Bc)->num('1')->isLessThan('10'); // true
```

isLessThanOrEqual
-----------------

[](#islessthanorequal)

It checks if a number is less than or equal to another number

```
(new Bc)->num('10')->isLessThanOrEqual('10'); // true
```

isEqual
-------

[](#isequal)

It checks if a number is equal to another number

```
(new Bc)->num('10')->isEqual('10'); // true
```

isDifferent
-----------

[](#isdifferent)

It checks if a number is different from another number

```
(new Bc)->num('10')->isDifferent('1'); // true
```

gt
--

[](#gt)

Same as `isGreaterThan`

```
(new Bc)->num('10')->gt('1'); // true
```

gte
---

[](#gte)

Same as `isGreaterThanOrEqual`

```
(new Bc)->num('10')->gte('10'); // true
```

lt
--

[](#lt)

Same as `isLessThan`

```
(new Bc)->num('1')->lt('10'); // true
```

lte
---

[](#lte)

Same as `isLessThanOrEqual`

```
(new Bc)->num('10')->lte('10'); // true
```

is
--

[](#is)

Same as `isEqual`

```
(new Bc)->num('10')->is('10'); // true
```

isNot
-----

[](#isnot)

Same as `isDifferent`

```
(new Bc)->num('10')->isNot('1'); // true
```

Runtime Exceptions
==================

[](#runtime-exceptions)

ScaleCannotBeUsedForOperation
-----------------------------

[](#scalecannotbeusedforoperation)

```
throw new \Tetthys\Bc\Exceptions\ScaleCannotBeUsedForOperation;
```

This is thrown when scale is less than 0.

ValueCannotBeUsedForOperation
-----------------------------

[](#valuecannotbeusedforoperation)

```
throw new \Tetthys\Bc\Exceptions\ValueCannotBeUsedForOperation;
```

This is thrown when value is not a number.

How to contribute and test in same environment?
===============================================

[](#how-to-contribute-and-test-in-same-environment)

### docker-compose up

[](#docker-compose-up)

```
docker-compose up

```

### attach shell to phptestenv container

[](#attach-shell-to-phptestenv-container)

```
bash ./run/shell/phptestenv.sh

```

### install dependencies with composer

[](#install-dependencies-with-composer)

```
composer install

```

### run test

[](#run-test)

```
./vendor/bin/pest

```

Or, you can run test from host OS using

```
bash ./run/test.sh

```

What I want to say
==================

[](#what-i-want-to-say)

It is currently June 2024.

I'm still learning commit and branch naming conventions and php composer rules.

I hope you understand even if some of the names are bad.

Thank you.

###  Health Score

29

—

LowBetter than 59% of packages

Maintenance32

Infrequent updates — may be unmaintained

Popularity13

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity53

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~6 days

Total

5

Last Release

684d ago

Major Versions

0.0.2.x-dev → 1.0.02024-07-03

### Community

Maintainers

![](https://www.gravatar.com/avatar/858f92afec0ff81e6888c9ae6f363b56ebf82e45a32d9e1b37341569c5d1b267?d=identicon)[tetthys](/maintainers/tetthys)

---

Top Contributors

[![tetthys](https://avatars.githubusercontent.com/u/115064626?v=4)](https://github.com/tetthys "tetthys (66 commits)")

---

Tags

decimalmatharithmeticintegerscalecalculationroundingnumberfloatprecisionbcmathoperationbc

###  Code Quality

TestsPest

### Embed Badge

![Health badge](/badges/tetthys-bc/health.svg)

```
[![Health](https://phpackages.com/badges/tetthys-bc/health.svg)](https://phpackages.com/packages/tetthys-bc)
```

###  Alternatives

[brick/math

Arbitrary-precision arithmetic library

2.1k504.0M277](/packages/brick-math)[aza/math

AzaMath - Anizoptera CMF mathematic component. Arbitrary precision arithmetic (for huge integers; BCMath wrapper) and universal convertor between positional numeral systems (supported bases from 2 to 62 inclusive, and systems with custom alphabet; pure PHP realisation, can use GMP and core PHP functions for speed optimization).

1921.9k1](/packages/aza-math)[php-decimal/php-decimal

Correctly-rounded arbitrary precision decimal floating point

781.0M9](/packages/php-decimal-php-decimal)[prestashop/decimal

Object-oriented wrapper/shim for BC Math PHP extension. Allows for arbitrary-precision math operations.

178.2M5](/packages/prestashop-decimal)[ionux/phactor

Phactor is a high-performance PHP implementation of the elliptic curve math functions required to generate &amp; verify private/public (asymmetric) EC keypairs and ECDSA signatures based on secp256k1 curve parameters. This library also includes a class to generate Service Identification Numbers (SINs) based on the published Identity Protocol v1 spec.

5275.0k30](/packages/ionux-phactor)[krowinski/bcmath-extended

Extends php BCMath lib for missing functions like floor, ceil, round, abs, min, max, rand for big numbers. Also wraps existing BCMath functions. (more http://php.net/manual/en/book.bc.php) Supports scientific notations

791.0M19](/packages/krowinski-bcmath-extended)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
