2017 © Pedro Peláez
 

library ez-math

A simple PHP wrapper math class for bcmath.

image

collab-corp/ez-math

A simple PHP wrapper math class for bcmath.

  • Thursday, February 1, 2018
  • by surgiie
  • Repository
  • 2 Watchers
  • 0 Stars
  • 10 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 6 Versions
  • 11 % Grown

The README.md

ez-math

Tests StyleCI, (*1)

A simple PHP wrapper math class for bcmath., (*2)

Installation

composer require collab-corp/ez-math, (*3)

Use

Simply new up the class with the value and call the needed methods:, (*4)


<?php use CollabCorp\EzMath; $math = (new Math(20))->add(2)->get(); //22.00 //or can cast object as string instead of calling get(); echo (string)(new Math(20))->add(2); // 22.00

Decimal/Precision Numbers

Since the default decimal places is 2, it is important to keep in mind that if you are going to be working with low/precise numbers, be sure to scale your decimal places high enough before performing operations so that the bcmath functions calculate the final result correctly:, (*5)

# via method
$number = (new Math(3.5))->setPlaces(3);

# or via constructor's 2nd argument:
$number = new Math(3.5, 3);

# can do any operations where expected result wont exceed 3 decimal places.

$math->toDecimalPercent()->get(); // returns 0.035 instead of 0.03 had we kept the default 2 places.

Method Chain

You can chain method calls as needed:, (*6)

<?php

use CollabCorp\EzMath;

$math = (new Math(20))
            ->add(2)
            ->subtract(2)
            ->divide(2)
            ->setPlaces(2)
            ->get(); //10.00

Set Decimal Places

By default, numbers scale to 2 decimal places. You can change to however many places you need to using the setPlaces method if working with higher precision numbers:, (*7)

$math = (new Math(20))->setPlaces(4)->get(); //22.0000

//or you can always specify the places via the class constructor
$math = (new Math(20, 4))->get(); //20.0000

Available Methods

setValue - set the value

$math = (new Math(20,0))->add(2)->get(); //22

$math = $math->setValue(25)->get();//25.00

setPlaces - round/scale to the specified decimal places

$math = (new Math(20,0))->add(2)->get(); //22

$math = $math->setPlaces(3)->get();//22.000

add - add a number to the value

$math = (new Math(20))->add(2); //22.0000....

subtract - subtract a number from the value

$math = (new Math(20))->subtract(2)->get(); //18.0000...

divide - divide the value by a number

$math = (new Math(20))->divide(2)->get(); //10.00000000

multiply - multiply the value by a number

$math = (new Math(20))->multiply(2)->get(); //40.00000000000

modulus - return the remainder after division(modulus)

$math = (new Math(8))->modulus(5)->get(); //3.00000....

toDecimalPercent - convert the value to a percent decimal

$math = (new Math(3.5))->setPlaces(3)->toDecimalPercent()->get(); // 0.035

percentageOf - get the given percent of the number


// get 7% of 3950 $tax = (new Math(3950))->percentageOf(7)->setPlaces(2)->get(); //276.50

squareRoot - get the square root of the value

$math = (new Math(16))->squareRoot()->get(); // 4.00

Contribute

Contributions are always welcome in the following manner:, (*8)

  • Issue Tracker
  • Pull Requests
  • Collab Corp Discord (Will send invite as requested)

License

The project is licensed under the MIT license., (*9)

The Versions

01/02 2018

dev-master

9999999-dev

A simple PHP wrapper math class for bcmath.

  Sources   Download

MIT

The Development Requires

by Sergio Compean
by Derrek Bertrand

php math bcmath

01/02 2018

v0.0.1

0.0.1.0

A simple PHP wrapper math class for bcmath.

  Sources   Download

MIT

The Development Requires

by Sergio Compean
by Derrek Bertrand

php math bcmath

11/12 2017

dev-dev

dev-dev

A simple PHP wrapper math class for bcmath.

  Sources   Download

MIT

The Development Requires

by Sergio Compean
by Derrek Bertrand

php math bcmath

11/12 2017

v1.0.2

1.0.2.0

A simple PHP wrapper math class for bcmath.

  Sources   Download

MIT

The Development Requires

by Sergio Compean
by Derrek Bertrand

php math bcmath

09/12 2017

v1.0.1

1.0.1.0

A simple PHP wrapper math class for bcmath.

  Sources   Download

MIT

The Development Requires

by Sergio Compean
by Derrek Bertrand

php math bcmath

24/11 2017

v1.0.0

1.0.0.0

A simple PHP wrapper math class for bcmath.

  Sources   Download

MIT

The Development Requires

by Sergio Compean
by Derrek Bertrand

php math bcmath