Search by

illegal / fluent-bcmath

illegalvuppi

Fluent BCMath is a PHP package designed to simplify the usage of PHP's bcmath functions in a more fluid and intuitive way. With its expressive syntax and Laravel compatibility, it aims to improve the developer experience while working with arbitrary precision arithmetic operations.

Package info

github.com/illegalstudio/fluent-bcmath

Documentation

pkg:composer/illegal/fluent-bcmath

Fund package maintenance!

nahime0

Statistics

Installs: 732

Dependents: 0

Suggesters: 0

Stars: 2

Open Issues: 0

v1.0.1 2023-04-10 22:02 UTC

This package is auto-updated.

Last update: 2026-10-01 13:29:19 UTC


README

Fluent BCMath logo

Fluent BCMath

Precise arithmetic. Fluent code.

Latest stable version Total downloads PHP 8.1 or newer License: MIT GitHub stars

Arbitrary precision · Chainable operations · Immutable numbers · PHP 8.1+

Fluent BCMath brings an expressive, fluent interface to PHP's BCMath extension. Chain arithmetic operations and compare numbers while keeping control over decimal precision.

Official Website

Installation

Install the package via composer:

composer require "illegal/fluent-bcmath"

Usage

You have two options to use the fluent interface.

In both cases, there are two arguments: the number and the scale. The number can be:

  • a string
  • an integer
  • a float
  • another BCNumber instance

The scale is an integer, which represents the number of digits after the decimal point.

All methods return a new BCNumber instance, so you can chain them.

For example:

use Illegal\FluentBCMath\BCNumber;
$num = new BCNumber('1.23', 2);

$num->add(2)->sub(2)->mul(2)->div(2)->mod(3)->pow(2)->sqrt();

1. Use the BCNumber class

use Illegal\FluentBCMath\BCNumber;

$num = new BCNumber('1.23', 2); // 1st argument is the number, 2nd argument is the scale

2. Use the fnum() helper function

$num = fnum('1.23', 2); // 1st argument is the number, 2nd argument is the scale

Available methods

$num = fnum(10, 2);

$num->add(2); // 12.00
$num->sub(2); // 8.00
$num->mul(2); // 20.00
$num->div(2); // 5.00
$num->mod(3); // 1.00
$num->pow(2); // 100.00
$num->sqrt(); // 3.16

$num->equals(10); // true
$num->greaterThan(5); // true
$num->greaterThanOrEqual(10); // true
$num->lessThan(15); // true
$num->lessThanOrEqual(10); // true
$num->isZero(); // false
$num->isPositive(); // true
$num->isNegative(); // false
$num->isEven(); // true
$num->isOdd(); // false
$num->abs(); // 10.00
$num->negate(); // -10.00
$num->min(5); // 5.00
$num->max(15); // 15.00
$num->clamp(5, 15); // 10.00

If methods

Each operation has an if method, which returns the result of the operation, if the condition is true.

$num = fnum(10, 2);

$num->addIf(2, false); // 10.00
$num->subIf(2, false); // 10.00
$num->mulIf(2, false); // 10.00
$num->divIf(2, false); // 10.00
$num->modIf(3, false); // 10.00
$num->powIf(2, false); // 10.00
$num->sqrtIf(false); // 10.00

Testing

./vendor/bin/pest

License

The MIT License (MIT). Please see License File for more information.