Fluent BCMath

A fluent interface for the PHP BCMath extension

Write more readable code, while maintaining the performance of the BCMath extension

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

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

Usage

Reference

A complete overview of the package's provided functionalities


There are two primary categories of methods

  • Calculations: Perform mathematical operations on the original BCNumber instance and return a new one.

  • Comparisons: Compare the number or assess its properties, such as whether it is positive, negative, and so forth.

Calculations

add

Increments the original number by adding a new value.

$num = fnum(10, 2);
$num->add(2); // 12.00

addIf

If the provided parameter is true, or the callback returns true, the original number is incremented by adding a new value.

$num = fnum(10, 2);
$num->addIf(2, fn() => true); // 12.00
$num->addIf(2, false); // 10.00

sub

Decrements the original number by subtracting a new value.

$num = fnum(10, 2);
$num->sub(2); // 8.00

subIf

If the provided parameter is true, or the callback returns true, the original number is decremented by subtracting a new value.

$num = fnum(10, 2);
$num->subIf(2, fn() => true); // 8.00
$num->subIf(2, false); // 10.00

mul

Multiplies the original number by a given value.

$num = fnum(10, 2);
$num->mul(2); // 20.00

mulf

If the provided parameter is true, or the callback returns true, the original number is multiplied by a new value.

$num = fnum(10, 2);
$num->mulIf(2, fn() => true); // 20.00
$num->mulIf(2, false); // 10.00

div

Divides the original number by a specified value.

$num = fnum(10, 2);
$num->div(2); // 5.00

divIf

If the provided parameter is true, or the callback returns true, the original number is divided by a new value.

$num = fnum(10, 2);
$num->divIf(2, fn() => true); // 5.00
$num->divIf(2, false); // 10.00

mod

Calculates the modulus of the original number using a specified divisor.

$num = fnum(10, 2);
$num->mod(3); // 1.00

modIf

If the provided parameter is true, or the callback returns true, the modulus of the original number is calculated using a new divisor.

$num = fnum(10, 2);
$num->modIf(3, fn() => true); // 1.00
$num->modIf(3, false); // 10.00

pow

Raises the original number to a specified power.

$num = fnum(10, 2);
$num->pow(2); // 100.00

powIf

If the provided parameter is true, or the callback returns true, the original number is raised to a new power.

$num = fnum(10, 2);
$num->powIf(2, fn() => true); // 100.00
$num->powIf(2, false); // 10.00

sqrt

Computes the square root of the original number.

$num = fnum(10, 2);
$num->sqrt(); // 3.16

sqrtIf

If the provided parameter is true, or the callback returns true, the square root of the original number is computed.

$num = fnum(10, 2);
$num->sqrtIf(fn() => true); // 3.16
$num->sqrtIf(false); // 10.00

abs

Calculates the absolute value of a number

$num = fnum(10, 2);
$num->abs(); // 10.00


Comparisons

equals

Determines if the original number is equal to the given number.

$num = fnum(10, 2);
$num->equals(10); // true
$num->equals(20); // false

greaterThan

Checks if the original number is greater than the given number.

$num = fnum(10, 2);
$num->greaterThan(5); // true
$num->greaterThan(20); // false

greaterThanOrEqual

Verifies if the original number is greater than or equal to the given one.

$num = fnum(10, 2);
$num->greaterThanOrEqual(5); // true
$num->greaterThanOrEqual(10); // true
$num->greaterThanOrEqual(20); // false

lessThan

Determines if a the original number is less than another.

$num = fnum(10, 2);
$num->lessThan(20); // true
$num->lessThan(5); // false

lessThanOrEqual

Confirms if the original number is less than or equal to another

$num = fnum(10, 2);
$num->lessThanOrEqual(5); // false
$num->lessThanOrEqual(10); // true
$num->lessThanOrEqual(20); // false

isZero

Checks if a number is zero.

$num = fnum(10, 2);
$num->isZero(); // false

isPositive

Verifies if a number is positive.

$num = fnum(10, 2);
$num->isPositive(); // true

isNegative

Determines if a number is negative.

$num = fnum(10, 2);
$num->isNegative(); // false

isEven

Confirms if a number is even.

$num = fnum(10, 2);
$num->isEven(); // true

isOdd

Checks if a number is odd.

$num = fnum(10, 2);
$num->isOdd(); // false

negate

Negates a number, changing its sign.

$num = fnum(10, 2);
$num->negate(); // -10.00

min

Finds the minimum value between the original number and another.

$num = fnum(10, 2);
$num->min(5); // 5.00

max

Determines the maximum value between the original number and another.

$num = fnum(10, 2);
$num->max(20); // 20.00

clamp

Constrains the original number within a specified range.

$num = fnum(10, 2);
$num->clamp(5, 20); // 10.00

Fluent BCMath is a project by illegal studio