A PHP class that represents a fraction. Converts to/from floats (0.25 ↔ ¼), simplifies fractions (⁴⁄₈ → ½), handles mathematical operations (½ + ⅓), supports negative fractions (−⅘), and does Unicode string output. Requires PHP 7.1 or above.
With Composer installed on your computer and initialized for your project, run this command in your project’s root directory:
composer require lamansky/fraction
Requires PHP 7.1 or above.
The library consists of a single class: Lamansky\Fraction\Fraction
.
$a
(int or float): The numerator of the fraction (the number on top).$b
(int or float): The denominator of the fraction (the number on bottom).- Optional:
$negative
(bool or int): If set totrue
or-1
(or any negative number), the fraction will be negative. If set tofalse
or1
(or any positive number), the fraction will be positive. If omitted, the fraction will be negative only if$a
or$b
is negative (but not both). If provided, the value of$negative
will override whatever sign values$a
or$b
may have.
Accepts one parameter (a float
number) and returns a Fraction
. The Fraction
will have the same sign value (positive/negative) as the float.
No parameters. Returns true
if the fraction is negative; otherwise false
.
No parameters. Returns -1
if the fraction is negative, or 1
if it is positive.
No parameters. Returns the numerator of the fraction (the number on top).
No parameters. Returns the integer component of a mixed fraction. A mixed fraction is one which is simplified to use a whole number (e.g. ⁵⁄₄ → 1¼). Example:
$f = new Fraction(7, 2);
echo $f->toString(); // 3 1/2
echo $f->getMixedInteger(); // 3
echo $f->getMixedNumerator(); // 1
echo $f->getDenominator(); // 2
If the fraction is not mixed (i.e. if the numerator is smaller than the denominator), this function will return 0
.
No parameters. Returns the numerator of a mixed fraction. A mixed fraction is one which is simplified to use a whole number (e.g. ⁵⁄₄ → 1¼). Example:
$f = new Fraction(5, 4);
echo $f->getNumerator(); // 5
echo $f->getMixedNumerator(); // 1
If the fraction is not mixed (i.e. if the numerator is smaller than the denominator), this function will return the normal numerator.
No parameters. Returns the denominator of the fraction (the number on bottom).
No parameters. Returns an array with two elements: the numerator and the denominator.
No parameters. Returns an array with three elements: the mixed-fraction integer, the mixed-fraction numerator, and the denominator. For example: for the fraction 2¼, it would return [2, 1, 4]
.
No parameters. Returns an ASCII string representation of the fraction.
$f = new Fraction(-5, 4);
echo $f->toString(); // '-1 1/4'
No parameters. Returns a Unicode string representation of the fraction.
$f = new Fraction(-5, 4);
echo $f->toUnicodeString(); // '−1¼'
No parameters. Divides the numerator by the denominator and returns a floating-point number.
$f = new Fraction(-5, 4);
echo $f->toFloat(); // -1.25
No parameters. Returns a Fraction
with the same numerator, denominator, and positive/negative sign.
No parameters. Clones the Fraction
, but makes it positive if it’s negative.
Returns a Fraction
that is the sum of the current fraction and $other
.
Note that if $other
is a negative fraction, this will end up being subtraction (just like in math).
Subtracts $other
from the current fraction and returns the result.
Multiplies the current fraction by $other
and returns the result.
Divides the current fraction by $other
and returns the result.
To run the development test suite, execute this command:
./vendor/phpunit/phpunit/phpunit tests