kdabrow/math

Objective wrapper over BCMath

Installs: 7

Dependents: 0

Suggesters: 0

Security: 0

Stars: 1

Watchers: 1

Forks: 0

Open Issues: 0

Type:package

1.0.0 2023-02-14 09:30 UTC

This package is auto-updated.

Last update: 2024-11-14 13:26:22 UTC


README

GitHub Workflow Status Packagist Version

Math

Objective wrapper over BCMath

Installation

Install via composer

composer require kdabrow/math

Usage

First declare mutable number. Second string is validated and mutated to correct format. Here are examples with converted number in the comment:

use Kdabrow\Math\Number;

new Number("100"); // 100

new Number("-100"); // -100

new Number("100,11"); // 100.11

new Number("100.11"); // 100.11

new Number("10,000.11"); // 10000.11

new Number("10 000.11"); // 10000.11

new Number("10'000.11"); // 10000.11

new Number(".11"); // 0.11

new Number(",11"); // 0.11

new Number("0,11"); // 0.11

new Number("0.11"); // 0.11

Then use API provided by Number class. All methods (with the 'sqrt' exception) accept arguments in a given formats:

// numeric string
$number->add('100');

// Number object
$number->add(new Number('100'));

// array of strings  
$number->add(['100', '100']);

// array of Number objects
$number->add([new Number('100'), new Number('100')]);
 
// object implementing ArrayAccess interface
$number->add(new Collection()); 

// many formats or same elements at the same time
$number->add('100', new Number('200'), ['100', new Number('200')]);  

Add

use Kdabrow\Math\Number;

$number = new Number('700');

$number->add('100'); // 800

Subtract

use Kdabrow\Math\Number;

$number = new Number('700');

$number->subtract('100'); // 600

Multiply

use Kdabrow\Math\Number;

$number = new Number('700');

$number->multiply('2'); // 1400

Divide

use Kdabrow\Math\Number;

$number = new Number('700');

$number->divide('2'); // 350

Square root

use Kdabrow\Math\Number;

$number = new Number('16');

$number->sqrt(); // 4

Power

use Kdabrow\Math\Number;

$number = new Number('2');

$number->pow('3'); // 8

Is equal

use Kdabrow\Math\Number;

$number = new Number('2');

$number->isEqual('3'); // false
$number->isEqual('2'); // true
$number->isEqual('1'); // false

Is equal or bigger

use Kdabrow\Math\Number;

$number = new Number('2');

$number->isEqualOrBigger('3'); // false
$number->isEqualOrBigger('2'); // true
$number->isEqualOrBigger('1'); // true

Is equal or lower

use Kdabrow\Math\Number;

$number = new Number('2');

$number->isEqualOrLower('3'); // true
$number->isEqualOrLower('2'); // true
$number->isEqualOrLower('1'); // false

Is lower

use Kdabrow\Math\Number;

$number = new Number('2');

$number->isLower('3'); // true
$number->isLower('2'); // true
$number->isLower('1'); // false

Is bigger

use Kdabrow\Math\Number;

$number = new Number('2');

$number->isBigger('3'); // false
$number->isBigger('2'); // true
$number->isBigger('1'); // true

Development

To run unit tests go into project folder and type in the console

vendor/bin/phpunit

Package contains docker container capable of running tests

docker compose run php vendor/bin/phpunit