PHP code example of elliotjreed / maths

1. Go to this page and download the library: Download elliotjreed/maths library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

elliotjreed / maths example snippets


use ElliotJReed\Maths\NumberImmutable;
use ElliotJReed\Maths\Number;

$numberImmutable = new NumberImmutable(10);
$newNumberImmutable = $number->multiply(3);
$numberImmutable->asFloat(); // 10.0
$newNumberImmutable->asFloat(); // 30.0

$number = new Number(15);
$newNumber = $number->multiply(3);
$number->asFloat(); // 45.0
$newNumber->asFloat(); // 45.0

use ElliotJReed\Maths\Number;

$number = new Number('123.5');

$number->multiply(2);
$number->add(3);
$number->divide(10);
$number->subtract(20);

$number->asFloat(); // 25.0
$number->asInteger(); // 25
$number->asString(); // '25'
$number->asString(1); // '25.0'
$number->asString(2); // '25.00'
$number->asString(3); // '25.000'

$number->isEqualTo(25); // true
$number->isLessThan(30); // true
$number->isGreaterThan(20); // true
$number->isGreaterThanOrEqualTo(25); // true
$number->isLessThanOrEqualTo(25); // true
$number->isZero(); // false

use ElliotJReed\Maths\Number;

$number = new Number(123.5);

$number->multiply(2)->add(3)->divide(10)->subtract(20);

$number->asFloat(); // 25.0
$number->asInteger(); // 25
$number->asString(); // '25'

use ElliotJReed\Maths\Number;

$number = new Number(123.5);

$number->multiply(new Number(2));
$number->add('3');
$number->divide(10.0);
$number->subtract(20);

$number->asFloat(); // 25.0
$number->asInteger(); // 25
$number->asString(); // '25'

use ElliotJReed\Maths\Number;

$number = new Number(10);
$anotherNumber = new Number($number);

$number->asFloat(); // 10.0
$anotherNumber->asFloat(); // 10.0

$number->add(10)->asFloat(); // 20.0
$anotherNumber->add(5)->asFloat(); // 15.0

use ElliotJReed\Maths\Number;

$number = new Number(1.125);

$number->roundToDecimalPlaces(2);

$number->asFloat(); // 1.13
$number->asInteger(); // 1
$number->asString(); // '1.13'

use ElliotJReed\Maths\Number;

$number = new Number(25);

$number->squareRoot();

$number->asFloat(); // 5.0
$number->asInteger(); // 5
$number->asString(); // '5'

use ElliotJReed\Maths\Number;

$number = new Number(5);

$number->raiseToPower(2);

$number->asFloat(); // 5.0
$number->asInteger(); // 5
$number->asString(); // '5'

use ElliotJReed\Maths\Number;

$number = new Number('25');

$number->increaseByPercentage(10);

$number->asFloat(); // 27.5
$number->asInteger(); // 28
$number->asInteger(PHP_ROUND_HALF_DOWN); // 27
$number->asString(); // '27.5'
$number->asString(2); // '27.50'

use ElliotJReed\Maths\Number;

$number = new Number(5.5);

$number->modulus(2.5);

$number->asFloat(); // 0.5
$number->asInteger(); // 1
$number->asInteger(PHP_ROUND_HALF_DOWN); // 0
$number->asString(); // '0.5'
$number->asString(2); // '0.50'

use ElliotJReed\Maths\Number;

$number = new Number('8.431e-05');

$number->asFloat(); // 0.00008431
$number->asString(); // '0.00008431'

use ElliotJReed\Maths\NumberImmutable;

$number = new NumberImmutable('123.5');

$number = $number->multiply(2);
$number = $number->add(3);
$number = $number->divide(10);
$number = $number->subtract(20);

$number->asFloat(); // 25.0
$number->asInteger(); // 25
$number->asString(); // '25'
$number->asString(1); // '25.0'
$number->asString(2); // '25.00'
$number->asString(3); // '25.000'

$number->isEqualTo(25); // true
$number->isLessThan(30); // true
$number->isGreaterThan(20); // true
$number->isGreaterThanOrEqualTo(25); // true
$number->isLessThanOrEqualTo(25); // true
$number->isZero(); // false

use ElliotJReed\Maths\NumberImmutable;

$number = new NumberImmutable(123.5);

$number = $number->multiply(2)->add(3)->divide(10)->subtract(20);

$number->asFloat(); // 25.0
$number->asInteger(); // 25
$number->asString(); // '25'

use ElliotJReed\Maths\NumberImmutable;

$number = new NumberImmutable(123.5);

$number = $number->multiply(new NumberImmutable(2));
$number = $number->add('3');
$number = $number->divide(10.0);
$number = $number->subtract(20);

$number->asFloat(); // 25.0
$number->asInteger(); // 25
$number->asString(); // '25'

use ElliotJReed\Maths\NumberImmutable;

$number = new NumberImmutable(1.125);

$number = $number->roundToDecimalPlaces(2);

$number->asFloat(); // 1.13
$number->asInteger(); // 1
$number->asString(); // '1.13'

use ElliotJReed\Maths\NumberImmutable;

$number = new NumberImmutable(25);

$number = $number->squareRoot();

$number->asFloat(); // 5.0
$number->asInteger(); // 5
$number->asString(); // '5'

use ElliotJReed\Maths\NumberImmutable;

$number = new NumberImmutable(5);

$number = $number->raiseToPower(2);

$number->asFloat(); // 5.0
$number->asInteger(); // 5
$number->asString(); // '5'

use ElliotJReed\Maths\NumberImmutable;

$number = new NumberImmutable('25');

$number = $number->increaseByPercentage(10);

$number->asFloat(); // 27.5
$number->asInteger(); // 28
$number->asInteger(PHP_ROUND_HALF_DOWN); // 27
$number->asString(); // '27.5'
$number->asString(2); // '27.50'

use ElliotJReed\Maths\NumberImmutable;

$number = new NumberImmutable(5.5);

$number = $number->modulus(2.5);

$number->asFloat(); // 0.5
$number->asInteger(); // 1
$number->asInteger(PHP_ROUND_HALF_DOWN); // 0
$number->asString(); // '0.5'
$number->asString(2); // '0.50'

use ElliotJReed\Maths\NumberImmutable;

$number = new NumberImmutable('8.431e-05');

$number->asFloat(); // 0.00008431
$number->asString(); // '0.00008431'
json
  "hp": "^8.1",
    "ext-bcmath": "*",
    "elliotjreed/maths": "^6.1"
  }
bash
php composer.phar install