xxc / fractionmath
Package for doing math operations with Fractions
Installs: 39
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 2
Forks: 0
Open Issues: 0
Type:project
Requires (Dev)
- phpspec/phpspec: ^4.2
README
Auto simplifies the fractions on initialisation. Installation:
composer require xxc/fractionmath:1.*
Usage:
$fraction = new Fraction(1,2); $fraction->display();
Available methods for a Fraction:
- numerator() - gets the numerator
- denominator() - gets the denominator
- integer() - gets the integer
- getGreatestCommonDivisor() - gets the GCD of the numetor and the denominator
- getFractionAsArray() - returns the fraction as array with keys numerator, denominator, integer
- getFractionAsObject() - returns the fraction as object
- display() - returns the html for a fraction
- parse() - static method for quick parsing strings to fractions
Works with irregular fractions aswell
$fraction = new Fraction(9,3); $fraction->getFractionAsArray(); /* returns ... array( 'numerator' => 9, 'denominator' => 3, 'integer' => 0 ); */
If you need you can parse strings to fractions with the static parse method. The method accepts a string param in the format integer-space-nominator-slash-deniminator - Example for valid inputs: 1/2; 5 1/2; 5/6; 666 1/5;
xxc\fractionmath\Fraction::parse('1/2'); \\returns a fraction with nominator 1 and denominator 2 xxc\fractionmath\Fraction::parse('3 1/2'); \\returns a fraction with integer 3,nominator 1 and denominator 2 \\you can chain methods like this xxc\fractionmath\Fraction::parse('2/4')->display(); \\returns html for a fraction 2/4
The Math class handles all the math operations - requires Fraction as inputs and returns a Fraction
Math operations:
- add()
- subtract()
- multiply()
- divide()
$fractionOne = new Fraction(1,3); $fractionTwo = new Fraction(1,3); $mathOperation = new Math(); $mathOperation->add($fractionOne, $fractionTwo); $mathOperation->subtract($fractionOne, $fractionTwo); $mathOperation->multiply($fractionOne, $fractionTwo); $mathOperation->divide($fractionOne, $fractionTwo);