PHP code example of xxc / fractionmath

1. Go to this page and download the library: Download xxc/fractionmath 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/ */

    

xxc / fractionmath example snippets


 composer 

$fraction = new Fraction(1,2);
$fraction->display();

$fraction = new Fraction(9,3);
$fraction->getFractionAsArray(); 
/*
returns ...
  array(
    'numerator' => 9,
    'denominator' => 3,
    'integer' => 0
    );
    */

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


$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);